-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Global Eyedropper for Windows #3801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f1391f1
76df245
87bbc14
225ec72
7abc34a
7640e2e
580e31f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use graphene_std::raster::color::Color; | ||
| use winit::dpi::PhysicalPosition; | ||
| use winit::event_loop::ActiveEventLoop; | ||
| use winit::window::WindowId; | ||
|
|
||
| pub(crate) struct GlobalEyedropperImpl { | ||
| primary: bool, | ||
| } | ||
|
|
||
| impl super::NativeEyedropper for GlobalEyedropperImpl { | ||
| fn new() -> Self { | ||
| Self { primary: true } | ||
| } | ||
|
|
||
| fn start(&mut self, _event_loop: &dyn ActiveEventLoop, _primary: bool) { | ||
| tracing::warn!("Global eyedropper is not yet supported on this platform"); | ||
| } | ||
|
|
||
| fn stop(&mut self) {} | ||
|
|
||
| fn is_active(&self) -> bool { | ||
| false | ||
| } | ||
|
|
||
| fn window_id(&self) -> Option<WindowId> { | ||
| None | ||
| } | ||
|
|
||
| fn update(&mut self, _position: PhysicalPosition<f64>) {} | ||
|
|
||
| fn render(&self) {} | ||
|
|
||
| fn sample_color(&self) -> Option<Color> { | ||
| None | ||
| } | ||
|
|
||
| fn is_primary(&self) -> bool { | ||
| self.primary | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this and the linux.rs be absracted ? Not super sure on this but the code is duplicated here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use graphene_std::raster::color::Color; | ||
| use winit::dpi::PhysicalPosition; | ||
| use winit::event_loop::ActiveEventLoop; | ||
| use winit::window::WindowId; | ||
|
|
||
| pub(crate) struct GlobalEyedropperImpl { | ||
| primary: bool, | ||
| } | ||
|
|
||
| impl super::NativeEyedropper for GlobalEyedropperImpl { | ||
| fn new() -> Self { | ||
| Self { primary: true } | ||
| } | ||
|
|
||
| fn start(&mut self, _event_loop: &dyn ActiveEventLoop, _primary: bool) { | ||
| tracing::warn!("Global eyedropper is not yet supported on this platform"); | ||
| } | ||
|
|
||
| fn stop(&mut self) {} | ||
|
|
||
| fn is_active(&self) -> bool { | ||
| false | ||
| } | ||
|
|
||
| fn window_id(&self) -> Option<WindowId> { | ||
| None | ||
| } | ||
|
|
||
| fn update(&mut self, _position: PhysicalPosition<f64>) {} | ||
|
|
||
| fn render(&self) {} | ||
|
|
||
| fn sample_color(&self) -> Option<Color> { | ||
| None | ||
| } | ||
|
|
||
| fn is_primary(&self) -> bool { | ||
| self.primary | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| use graphene_std::raster::color::Color; | ||
| use winit::dpi::PhysicalPosition; | ||
| use winit::event_loop::ActiveEventLoop; | ||
| use winit::window::WindowId; | ||
|
|
||
| pub(crate) trait NativeEyedropper { | ||
| fn new() -> Self; | ||
| fn start(&mut self, event_loop: &dyn ActiveEventLoop, primary: bool); | ||
| fn stop(&mut self); | ||
| fn is_active(&self) -> bool; | ||
| fn window_id(&self) -> Option<WindowId>; | ||
| fn update(&mut self, position: PhysicalPosition<f64>); | ||
| fn render(&self); | ||
| fn sample_color(&self) -> Option<Color>; | ||
| fn is_primary(&self) -> bool; | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| mod win; | ||
| #[cfg(target_os = "windows")] | ||
| use win as native; | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| mod linux; | ||
| #[cfg(target_os = "linux")] | ||
| use linux as native; | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| mod mac; | ||
| #[cfg(target_os = "macos")] | ||
| use mac as native; | ||
|
|
||
| pub(crate) type GlobalEyedropper = native::GlobalEyedropperImpl; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| use graphene_std::raster::color::Color; | ||
| use winit::dpi::{PhysicalPosition, PhysicalSize}; | ||
| use winit::event_loop::ActiveEventLoop; | ||
| use winit::raw_window_handle::HasWindowHandle; | ||
| use winit::window::{Window, WindowAttributes, WindowId}; | ||
| use windows::Win32::Foundation::{HWND, RECT}; | ||
| use windows::Win32::Graphics::Gdi::{ | ||
| CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, DeleteObject, FrameRect, GetDC, GetStockObject, ReleaseDC, SelectObject, StretchBlt, BLACK_BRUSH, SRCCOPY, | ||
| }; | ||
| use windows::Win32::UI::WindowsAndMessaging::GetCursorPos; | ||
|
|
||
| const MAGNIFIER_RES: u32 = 11; | ||
| const MAGNIFIER_SIZE: u32 = 110; | ||
|
|
||
| pub(crate) struct GlobalEyedropperImpl { | ||
| window: Option<Window>, | ||
| primary: bool, | ||
| } | ||
|
|
||
| impl super::NativeEyedropper for GlobalEyedropperImpl { | ||
| fn new() -> Self { | ||
| Self { | ||
| window: None, | ||
| primary: true, | ||
| } | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn stop(&mut self) { | ||
| self.window = None; | ||
| } | ||
|
|
||
| fn is_active(&self) -> bool { | ||
| self.window.is_some() | ||
| } | ||
|
|
||
| fn window_id(&self) -> Option<WindowId> { | ||
| self.window.as_ref().map(|w| w.id()) | ||
| } | ||
|
|
||
| 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 render(&self) { | ||
| let Some(_window) = &self.window else { return }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the window variable is unused, consider using this
|
||
|
|
||
| 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); | ||
|
|
||
| let mem_dc = CreateCompatibleDC(desktop_dc); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you are doing this create and delete cycle on every render, Consider using the below code for optimization , basically instead do caching in GlobalEyedropperImpl struct . Since render happens very frequently , this should be a good caching optimization |
||
| let bitmap = CreateCompatibleBitmap(desktop_dc, MAGNIFIER_RES as i32, MAGNIFIER_RES as i32); | ||
| let old_bitmap = SelectObject(mem_dc, 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(); | ||
|
|
||
| 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(); | ||
|
|
||
| SelectObject(mem_dc, old_bitmap); | ||
| let _ = DeleteObject(bitmap); | ||
| let _ = DeleteDC(mem_dc); | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| 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)) | ||
| } | ||
| } | ||
|
|
||
| fn is_primary(&self) -> bool { | ||
| self.primary | ||
| } | ||
| } | ||
|
|
||
| impl GlobalEyedropperImpl { | ||
| 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, | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The global eyedropper window is being recreated on every
PointerMoveevent when the cursor is outside the viewport becauseeyedropper_tool.rsspams this message. This causes significant performance overhead and resource churn. You should check if the eyedropper is already active before scheduling a start event.