Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ accelerated_paint = ["cef/accelerated_osr"]
# Local dependencies
graphite-desktop-wrapper = { path = "wrapper" }
graphite-desktop-embedded-resources = { path = "embedded-resources", optional = true }
graphene-std = { workspace = true }

wgpu = { workspace = true }
winit = { workspace = true, features = [
Expand Down
47 changes: 46 additions & 1 deletion desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(crate) struct App {
start_render_sender: SyncSender<()>,
web_communication_initialized: bool,
web_communication_startup_buffer: Vec<Vec<u8>>,
global_eyedropper: crate::global_eyedropper::GlobalEyedropper,
persistent_data: PersistentData,
cli: Cli,
startup_time: Option<Instant>,
Expand Down Expand Up @@ -114,6 +115,7 @@ impl App {
start_render_sender,
web_communication_initialized: false,
web_communication_startup_buffer: Vec::new(),
global_eyedropper: crate::global_eyedropper::GlobalEyedropper::new(),
persistent_data,
cli,
startup_time: None,
Expand Down Expand Up @@ -416,6 +418,15 @@ impl App {
DesktopFrontendMessage::Restart => {
self.exit(Some(ExitReason::Restart));
}
DesktopFrontendMessage::GlobalEyedropper { open, primary } => {
if open {
if !self.global_eyedropper.is_active() {
self.app_event_scheduler.schedule(AppEvent::StartGlobalEyedropper { primary });
}
} else {
self.global_eyedropper.stop();
}
}
Comment on lines 421 to 429
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The global eyedropper window is being recreated on every PointerMove event when the cursor is outside the viewport because eyedropper_tool.rs spams this message. This causes significant performance overhead and resource churn. You should check if the eyedropper is already active before scheduling a start event.

			DesktopFrontendMessage::GlobalEyedropper { open, primary } => {
				if open {
					if !self.global_eyedropper.is_active() {
						self.app_event_scheduler.schedule(AppEvent::StartGlobalEyedropper { primary });
					}
				} else {
					self.global_eyedropper.stop();
				}
			}

}
}

Expand Down Expand Up @@ -491,6 +502,9 @@ impl App {
tracing::info!("Exiting main event loop");
event_loop.exit();
}
AppEvent::StartGlobalEyedropper { primary } => {
self.global_eyedropper.start(event_loop, primary);
}
#[cfg(target_os = "macos")]
AppEvent::MenuEvent { id } => {
self.dispatch_desktop_wrapper_message(DesktopWrapperMessage::MenuEvent { id });
Expand Down Expand Up @@ -523,7 +537,33 @@ impl ApplicationHandler for App {
}
}

fn window_event(&mut self, _event_loop: &dyn ActiveEventLoop, _window_id: WindowId, event: WindowEvent) {
fn window_event(&mut self, event_loop: &dyn ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
if Some(window_id) == self.global_eyedropper.window_id() {
match event {
WindowEvent::PointerMoved { position, .. } => {
self.global_eyedropper.update(position);
}
WindowEvent::PointerButton {
state: ElementState::Pressed,
button: ButtonSource::Mouse(MouseButton::Left),
..
} => {
if let Some(color) = self.global_eyedropper.sample_color() {
let primary = self.global_eyedropper.is_primary();
let message = DesktopWrapperMessage::PickGlobalColor { color, primary };
self.dispatch_desktop_wrapper_message(message);
}
self.global_eyedropper.stop();
}
WindowEvent::KeyboardInput { .. } => {
// TODO: Support Esc to abort
self.global_eyedropper.stop();
}
_ => {}
}
return;
}

// Handle pointer lock release
if let Some(pointer_lock_position) = self.pointer_lock_position
&& let WindowEvent::PointerButton {
Expand Down Expand Up @@ -554,6 +594,11 @@ impl ApplicationHandler for App {
self.resize();
}
WindowEvent::RedrawRequested => {
if Some(window_id) == self.global_eyedropper.window_id() {
self.global_eyedropper.render();
return;
}

#[cfg(target_os = "macos")]
self.resize();

Expand Down
3 changes: 3 additions & 0 deletions desktop/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub(crate) enum AppEvent {
DesktopWrapperMessage(DesktopWrapperMessage),
NodeGraphExecutionResult(NodeGraphExecutionResult),
Exit,
StartGlobalEyedropper {
primary: bool,
},
#[cfg(target_os = "macos")]
MenuEvent {
id: String,
Expand Down
180 changes: 180 additions & 0 deletions desktop/src/global_eyedropper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
use winit::event_loop::ActiveEventLoop;
use winit::window::{Window, WindowAttributes, WindowId};
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::raw_window_handle::HasWindowHandle;
use windows::Win32::Graphics::Gdi::{
CreateCompatibleBitmap, CreateCompatibleDC, CreateSolidBrush, DeleteDC, DeleteObject, FillRect, FrameRect, GetDC, GetStockObject, ReleaseDC, SelectObject, StretchBlt,
BLACK_BRUSH, SRCCOPY,
};
use windows::Win32::Foundation::{HWND, RECT};
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;
use graphene_std::raster::color::Color;

const MAGNIFIER_RES: u32 = 11;
const MAGNIFIER_SIZE: u32 = 110;

pub struct GlobalEyedropper {
window: Option<Window>,
primary: bool,
}

impl GlobalEyedropper {
pub fn new() -> Self {
Self {
window: None,
primary: true,
}
}

pub fn start(&mut self, event_loop: &dyn ActiveEventLoop, primary: bool) {
if self.is_active() {
return;
}

self.primary = primary;
let attributes = WindowAttributes::default()
.with_title("Graphite Eyedropper")
.with_decorations(false)
.with_transparent(true)
.with_always_on_top(true)
.with_visible(false);

match event_loop.create_window(attributes) {
Ok(window) => {
self.window = Some(window);
}
Err(e) => {
tracing::error!("Failed to create global eyedropper window: {:?}", e);
}
}
}

pub fn stop(&mut self) {
self.window = None;
}

pub fn is_active(&self) -> bool {
self.window.is_some()
}

pub fn window_id(&self) -> Option<WindowId> {
self.window.as_ref().map(|w| w.id())
}

pub fn update(&mut self, position: PhysicalPosition<f64>) {
let Some(window) = &self.window else { return };

let size = PhysicalSize::new(MAGNIFIER_SIZE, MAGNIFIER_SIZE);
window.set_outer_position(PhysicalPosition::new(position.x - size.width as f64 / 2., position.y - size.height as f64 / 2.));
window.set_min_surface_size(Some(size.into()));
window.set_visible(true);
window.request_redraw();
}

fn window_hwnd(&self) -> HWND {
let Some(window) = &self.window else {
return HWND::default();
};
HWND(match window.window_handle().unwrap().as_raw() {
winit::raw_window_handle::RawWindowHandle::Win32(handle) => handle.hwnd.get() as isize,
_ => 0,
})
}

pub fn render(&self) {
let Some(_window) = &self.window else { return };

unsafe {
let mut pt = Default::default();
if GetCursorPos(&mut pt).is_err() {
return;
}

let pixel_size = MAGNIFIER_SIZE / MAGNIFIER_RES;
let half = MAGNIFIER_RES as i32 / 2;

let desktop_dc = GetDC(HWND::default());
let window_hwnd = self.window_hwnd();
let window_dc = GetDC(window_hwnd);

// Capture the screen area into a memory DC, then stretch it onto the window
let mem_dc = CreateCompatibleDC(desktop_dc);
let bitmap = CreateCompatibleBitmap(desktop_dc, MAGNIFIER_RES as i32, MAGNIFIER_RES as i32);
let old_bitmap = SelectObject(mem_dc, bitmap);

// Copy 11x11 pixels from the screen around the cursor into the memory bitmap
StretchBlt(
mem_dc,
0,
0,
MAGNIFIER_RES as i32,
MAGNIFIER_RES as i32,
desktop_dc,
pt.x - half,
pt.y - half,
MAGNIFIER_RES as i32,
MAGNIFIER_RES as i32,
SRCCOPY,
)
.ok();

// Now stretch the small bitmap onto the window DC to get the magnified view
StretchBlt(
window_dc,
0,
0,
MAGNIFIER_SIZE as i32,
MAGNIFIER_SIZE as i32,
mem_dc,
0,
0,
MAGNIFIER_RES as i32,
MAGNIFIER_RES as i32,
SRCCOPY,
)
.ok();

// Clean up memory DC and bitmap
SelectObject(mem_dc, old_bitmap);
let _ = DeleteObject(bitmap);
let _ = DeleteDC(mem_dc);

// Draw crosshair border on the center pixel
let mid = MAGNIFIER_RES / 2;
let rect = RECT {
left: (mid * pixel_size) as i32,
top: (mid * pixel_size) as i32,
right: ((mid + 1) * pixel_size) as i32,
bottom: ((mid + 1) * pixel_size) as i32,
};
let black_brush = GetStockObject(BLACK_BRUSH);
FrameRect(window_dc, &rect, black_brush.into());

ReleaseDC(HWND::default(), desktop_dc);
ReleaseDC(window_hwnd, window_dc);
}
}

pub fn sample_color(&self) -> Option<Color> {
unsafe {
let mut pt = Default::default();
if GetCursorPos(&mut pt).is_err() {
return None;
}

let hdc = GetDC(HWND::default());
let pixel = windows::Win32::Graphics::Gdi::GetPixel(hdc, pt.x, pt.y);
ReleaseDC(HWND::default(), hdc);

let r = (pixel.0 & 0xFF) as f32 / 255.0;
let g = ((pixel.0 >> 8) & 0xFF) as f32 / 255.0;
let b = ((pixel.0 >> 16) & 0xFF) as f32 / 255.0;

Some(Color::from_rgbaf32_unchecked(r, g, b, 1.0))
}
}

pub fn is_primary(&self) -> bool {
self.primary
}
}
1 change: 1 addition & 0 deletions desktop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod cef;
mod cli;
mod dirs;
mod event;
mod global_eyedropper;
mod gpu_context;
mod persist;
mod preferences;
Expand Down
9 changes: 9 additions & 0 deletions desktop/wrapper/src/handle_desktop_wrapper_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ pub(super) fn handle_desktop_wrapper_message(dispatcher: &mut DesktopWrapperMess
DesktopWrapperMessage::Input(message) => {
dispatcher.queue_editor_message(EditorMessage::InputPreprocessor(message));
}
DesktopWrapperMessage::PickGlobalColor { color, primary } => {
dispatcher.queue_editor_message(ToolMessage::SelectWorkingColor { color, primary }.into());
let end_message = if primary {
EyedropperToolMessage::SamplePrimaryColorEnd
} else {
EyedropperToolMessage::SampleSecondaryColorEnd
};
dispatcher.queue_editor_message(end_message.into());
}
DesktopWrapperMessage::FileDialogResult { path, content, context } => match context {
OpenFileDialogContext::Open => {
dispatcher.queue_desktop_wrapper_message(DesktopWrapperMessage::OpenFile { path, content });
Expand Down
3 changes: 3 additions & 0 deletions desktop/wrapper/src/intercept_frontend_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub(super) fn intercept_frontend_message(dispatcher: &mut DesktopWrapperMessageD
FrontendMessage::WindowRestart => {
dispatcher.respond(DesktopFrontendMessage::Restart);
}
FrontendMessage::GlobalEyedropper { open, primary } => {
dispatcher.respond(DesktopFrontendMessage::GlobalEyedropper { open, primary });
}
m => return Some(m),
}
None
Expand Down
8 changes: 8 additions & 0 deletions desktop/wrapper/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,19 @@ pub enum DesktopFrontendMessage {
WindowHideOthers,
WindowShowAll,
Restart,
GlobalEyedropper {
open: bool,
primary: bool,
},
}

pub enum DesktopWrapperMessage {
FromWeb(Box<EditorMessage>),
Input(InputMessage),
PickGlobalColor {
color: graphene_std::raster::color::Color,
primary: bool,
},
FileDialogResult {
path: PathBuf,
content: Vec<u8>,
Expand Down
4 changes: 4 additions & 0 deletions editor/src/messages/frontend/frontend_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ pub enum FrontendMessage {
UpdateUIScale {
scale: f64,
},
GlobalEyedropper {
open: bool,
primary: bool,
},

#[cfg(not(target_family = "wasm"))]
RenderOverlays {
Expand Down
14 changes: 13 additions & 1 deletion editor/src/messages/tool/tool_messages/eyedropper_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,37 @@ impl Fsm for EyedropperToolFsmState {
// Sampling -> Sampling
(EyedropperToolFsmState::SamplingPrimary | EyedropperToolFsmState::SamplingSecondary, EyedropperToolMessage::PointerMove) => {
let mouse_position = viewport.logical(input.mouse.position);
let primary = self == EyedropperToolFsmState::SamplingPrimary;
if viewport.is_in_bounds(mouse_position + viewport.offset()) {
update_cursor_preview(responses, tool_data, input, global_tool_data, None);
#[cfg(not(target_family = "wasm"))]
responses.add(FrontendMessage::GlobalEyedropper { open: false, primary });
} else {
#[cfg(not(target_family = "wasm"))]
responses.add(FrontendMessage::GlobalEyedropper { open: true, primary });
#[cfg(target_family = "wasm")]
disable_cursor_preview(responses, tool_data);
}

self
}
// Sampling -> Ready
(EyedropperToolFsmState::SamplingPrimary, EyedropperToolMessage::SamplePrimaryColorEnd) | (EyedropperToolFsmState::SamplingSecondary, EyedropperToolMessage::SampleSecondaryColorEnd) => {
let set_color_choice = if self == EyedropperToolFsmState::SamplingPrimary { "Primary" } else { "Secondary" }.to_string();
let primary = self == EyedropperToolFsmState::SamplingPrimary;
let set_color_choice = if primary { "Primary" } else { "Secondary" }.to_string();
update_cursor_preview(responses, tool_data, input, global_tool_data, Some(set_color_choice));
disable_cursor_preview(responses, tool_data);
#[cfg(not(target_family = "wasm"))]
responses.add(FrontendMessage::GlobalEyedropper { open: false, primary });

EyedropperToolFsmState::Ready
}
// Any -> Ready
(_, EyedropperToolMessage::Abort) => {
let primary = self == EyedropperToolFsmState::SamplingPrimary;
disable_cursor_preview(responses, tool_data);
#[cfg(not(target_family = "wasm"))]
responses.add(FrontendMessage::GlobalEyedropper { open: false, primary });

EyedropperToolFsmState::Ready
}
Expand Down