|
I'm trying to write an app that will allow users to identify a section of a screen. I'd like to mimic the way the built-in screen shot utility works: Dimming the screen, and allowing the user to click and drag a rectangle that then shows at full brightness. I need a way to get the coordinates and screen id of the user selected bounding box. In SwiftUI. I've seen some example code (ancient) from Apple called DrawMouseBoxView, but I'm at a loss as to how to do this in SwiftUI (not AppKit). Any pointers? |
|
|
|
I was wondering if you could get a reference to a Window in a SwifUI app and you can, like this
you can then get it via the environmentObject paramater. To make this work the way you want, the window needs to be at a high level and have a non opaque background and be the size of the sreen. There are settings for the windo that can do that. |
|
@eoinnorris Yep, I need to be able let the user draw the rectangle anywhere on the screen, not just in the app's window(s). It's very much like the screen shot example. I need to be able to let the user decide which portion of their screen will be mirrored. |
|
@eoinnorris Yep, I need to be able let the user draw the rectangle anywhere on the screen, not just in the app's window(s). It's very much like the screen shot example. I need to be able to let the user decide which portion of their screen will be mirrored. |
|
@eoinnorris Let me see if i understand your second response correctly...
In general, I know how to do #2, I think. But I'm not certain how to force a window to stay on top until some user interaction has occured? I'm also not clear on how to make a window take up the full screen on all screens. (Think multi-monitor setups) I want to thank you for the pointers and code you've provided. I'm sure I'm an anoying newb, not quite putting together the perls you're laying down. Any future help is greatly appreciated. |
|
You are going to have to work with a bit of appKit there to get the multiple monitors. Take a look at NSScreens. I tried this
I tried that and I got the dimensions of my main screen. The other dimensions are relative to the main screen so in theory setting the the windows you generate to the frame of those values will cover all the screens. Thats problem 1. I am not certain how to create these windows on SwiftUI - on my app it just works with the contentview, maybe use windowgroup - there is some mention of that on the web. So on launch if can create the windows, you need to iterate the screens and create a window on each screen. If you are stuck there I might be able to help. I might need auto window generation in my own app soon. For the window opacity you need something ike this
Levels are set with window.level. Take the highest from here. https://developer.apple.com/documentation/appkit/nswindowlevel?language=objc And to make it borderless take a look at the styleMask https://developer.apple.com/documentation/appkit/nswindow/stylemask |
|
And to ignore events use
which you can toggle if the user has hit a key combo. This is all tricky enough in AppKit where there is more access to the fundamentals of an NSWindow but thats the general Idea I think. |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.