-
-
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
Open
yush-1018
wants to merge
5
commits into
GraphiteEditor:master
Choose a base branch
from
yush-1018:feat/global-eyedropper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1391f1
feat(desktop): implement global eyedropper with magnification for Win…
yush-1018 76df245
Merge branch 'master' into feat/global-eyedropper
yush-1018 87bbc14
fix: address PR review - use StretchBlt, proper GDI brushes, guard re…
yush-1018 225ec72
Merge branch 'feat/global-eyedropper' of https://github.com/yush-1018…
yush-1018 e8103c5
Merge branch 'master' into feat/global-eyedropper
yush-1018 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.