Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 44 additions & 1 deletion desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 @@ -105,6 +106,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,
exit_reason: ExitReason::Shutdown,
Expand Down Expand Up @@ -402,6 +404,13 @@ impl App {
DesktopFrontendMessage::Restart => {
self.exit(Some(ExitReason::Restart));
}
DesktopFrontendMessage::GlobalEyedropper { open, primary } => {
if open {
self.app_event_scheduler.schedule(AppEvent::StartGlobalEyedropper { primary });
} else {
self.global_eyedropper.stop();
}
}
Comment on lines +434 to +442
Copy link
Copy Markdown
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 @@ -477,6 +486,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 @@ -509,7 +521,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 @@ -540,6 +578,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
138 changes: 138 additions & 0 deletions desktop/src/global_eyedropper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
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::{GetDC, ReleaseDC, GetPixel, COLORREF, BitBlt, CreateCompatibleDC, CreateCompatibleBitmap, SelectObject, DeleteDC, DeleteObject, SRCCOPY, HDC};
use windows::Win32::Foundation::{HWND, RECT};
use windows::Win32::UI::WindowsAndMessaging::{GetCursorPos, GetDesktopWindow, GetWindowRect};
use graphene_std::raster::color::Color;

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) {
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); // We'll show it and move it in the first update

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(110, 110);
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();
}

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

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

let res = 11;
let pixel_size = size.width / res;

let desktop_dc = GetDC(HWND::default());
let window_dc = GetDC(HWND(match window.window_handle().unwrap().as_raw() {
winit::raw_window_handle::RawWindowHandle::Win32(handle) => handle.hwnd.get() as isize,
_ => 0,
}));

for y in 0..res {
for x in 0..res {
let sx = pt.x - (res as i32 / 2) + x as i32;
let sy = pt.y - (res as i32 / 2) + y as i32;
let color = GetPixel(desktop_dc, sx, sy);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Calling GetPixel 121 times per frame is very inefficient. Additionally, since the magnifier window is moved to the cursor position before rendering, GetPixel on the desktop DC will likely sample the magnifier window itself rather than the content beneath it. Consider capturing the screen area into a memory DC before moving/showing the magnifier, or using StretchBlt which is significantly faster.


let rect = RECT {
left: (x * pixel_size) as i32,
top: (y * pixel_size) as i32,
right: ((x + 1) * pixel_size) as i32,
bottom: ((y + 1) * pixel_size) as i32,
};
windows::Win32::Graphics::Gdi::FillRect(window_dc, &rect, windows::Win32::Graphics::Gdi::HBRUSH((color.0 + 1) as isize));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

Using HBRUSH((color.0 + 1) as isize) is incorrect for arbitrary colors. In GDI, this syntax only works for system color indices (e.g., COLOR_WINDOW + 1). For arbitrary RGB colors, you must create a solid brush using CreateSolidBrush and ensure it is deleted afterwards to avoid GDI object leaks.

					let brush = windows::Win32::Graphics::Gdi::CreateSolidBrush(color);
					windows::Win32::Graphics::Gdi::FillRect(window_dc, &rect, brush);
					let _ = windows::Win32::Graphics::Gdi::DeleteObject(brush);

}
}

let mid = 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,
};
windows::Win32::Graphics::Gdi::FrameRect(window_dc, &rect, windows::Win32::Graphics::Gdi::HBRUSH(1));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

HBRUSH(1) corresponds to COLOR_SCROLLBAR + 1. If you intended to draw a black or neutral frame, it's better to use a stock object like BLACK_BRUSH.

			let brush = windows::Win32::Graphics::Gdi::GetStockObject(windows::Win32::Graphics::Gdi::BLACK_BRUSH);
			windows::Win32::Graphics::Gdi::FrameRect(window_dc, &rect, windows::Win32::Graphics::Gdi::HBRUSH(brush.0));


ReleaseDC(HWND::default(), desktop_dc);
ReleaseDC(HWND(match window.window_handle().unwrap().as_raw() {
winit::raw_window_handle::RawWindowHandle::Win32(handle) => handle.hwnd.get() as isize,
_ => 0,
}), 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 = 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(r, g, b, 1.0).unwrap())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since the RGB values are derived from u8 division, they are guaranteed to be within the valid range [0, 1]. You can use from_rgbaf32_unchecked to avoid the overhead of checks and the unwrap() call.

			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