When developing pocketed input method,I met such a thing,That is, as a candidate bar window will go beyond the screen when the edge of the screen! and so,In the display window when I do extra check the coordinates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
if visiableCandidateCells.isEmpty { if let screenframe = NSScreen.main?.visibleFrame { if screenframe.width < location.x + size.width { location.x -= location.x + size.width - screenframe.width } } } else { if let screenframe = NSScreen.main?.visibleFrame { if screenframe.width < location.x + self.window!.frame.size.width { location.x -= location.x + self.window!.frame.size.width - screenframe.width } } } if location.y < 50 { location.y += 35 + 35 } |
Anyway,That is, if you count the coordinates of their width exceeds the width of the screen,Norway put it back。
but,Such treatment does not work well in a multi-monitor,This case, since the multi display coordinate caused:
macOS multi-display coordinate Mechanism
As shown in FIG,When a user has a plurality of displays,Display coordinates are merged together,Also left negative right to follow the rules,In this situation,When your user typing on the right side of the screen,Once the position coordinates of the insertion point is greater than the maximum screen coordinates (Remember,The maximum value of the right side of the screen coordinates of which a greater width and height),Window will not continue right away。
Then,To be corrected for different screen coordinates,I.e., not used as a basis to judge the size of the screen resolution,But to use the screen width and height plus the current screen origin offset! The modified code then follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
if visiableCandidateCells.isEmpty { if let screenframe = NSScreen.main?.frame { if screenframe.width+screenframe.origin.x < location.x + size.width { location.x -= location.x + size.width - (screenframe.width+screenframe.origin.x) } if location.y < screenframe.origin.y + 50 { location.y += 35 + 35 } } } else { if let screenframe = NSScreen.main?.frame { if screenframe.width+screenframe.origin.x < location.x + self.window!.frame.size.width { location.x -= location.x + self.window!.frame.size.width - (screenframe.width+screenframe.origin.x) } if location.y < screenframe.origin.y + 50 { location.y += 35 + 35 } } } |
Such,On multi-monitor,Regardless of the user to arrange the display arrangement,Candidate window will not go beyond the edge of the current screen。
app window full-screen problem
There is a pit worthy to talk,Is the work space on the macOS,Multiple desktops mechanism has been a good place macOS,Rare - you do not have any api to check user space in which the ......
It would have been nothing,Available in multi-screen mode on a problem - as long as a user app in full-screen external screen,You can not get to the correct screen size - so there is nothing to correct the above。
When you are a full-screen app,In this mode,Get the screen size is always the size of the main screen,No matter which screen is on。
Fortunately,,Get to the point coordinate input or correct,So I can actually look through all the screens to the insertion point on which screen,And then to offset processing based on this screen。
so,The complete code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
var currentScreen = NSScreen.main for scr in NSScreen.screens { if scr.frame.contains(location) { currentScreen = scr break } } if visiableCandidateCells.isEmpty { if let screenframe = currentScreen?.frame { if screenframe.width+screenframe.origin.x < location.x + size.width { location.x -= location.x + size.width - (screenframe.width+screenframe.origin.x) } if location.y < screenframe.origin.y + 50 { location.y += 35 + 35 } } } else { if let screenframe = currentScreen?.frame { if screenframe.width+screenframe.origin.x < location.x + self.window!.frame.size.width { location.x -= location.x + self.window!.frame.size.width - (screenframe.width+screenframe.origin.x) } if location.y < screenframe.origin.y + 50 { location.y += 35 + 35 } } } |
Original article written by LogStudio:R0uter's Blog » Analyzing a multi-monitor window position macOS
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2771.html
Developer Hello,Bought macOS2 input method you develop,very useful。
But where there's a little bit less than:I macOS (10.13.6) + an external monitor。I am a full-screen on the external monitor APP,When prompted with a shift in the English state switch ( "En" or "in" switch) can not be represented。When no external monitor only macOS,Fullscreen APP also has this phenomenon。for reference only。
Recognizing this problem,I will proceed to repair。Thanks Feedback。