How do I get the bounds of the display for the Mac OS X application with keyboard/mouse focus? - swift

I am using the following script to 'maximize' the current application window on my mac.
It works fine when the frontmost application window is on the main / larger external display, or when my laptop is not connected to an external display.
However, when I am connected to an external display, and the window I am trying to maximize is on my laptop display (not the external, primary display), this script ends up enlarging the window to the dimensions of the larger display.
try
tell application "Finder" to set b to bounds of window of desktop
try
tell application (path to frontmost application as text)
set bounds of window 1 to {item 1 of b, 22, item 3 of b, item 4 of b}
end tell
on error
tell application "System Events" to tell window 1 of (process 1 where it is frontmost)
try
set position to {0, 22}
set size to {item 3 of b, (item 4 of b) - 22}
on error
click (button 1 of window 1 where subrole is "AXZoomButton")
end try
end tell
end try
end try
I believe the issue is the way I am getting the desired bounds, b:
bounds of window of desktop
This seems to always return the bounds of the primary display.
What I need is a way to detect the bounds of the display for the current running application that has keyboard and mouse in focus.
NB: I am totally cool if the solution to this is in Swift and not Apple Script.

Have you considered this:
tell application (path to frontmost application as text)
set zoomed of window 1 to true
end tell

Related

Eclipse RCP Center Dialog on Active Monitor

I am trying to center a dialog in my Eclipse RCP application, and I tried different methods to center the dialog.
Is there an easy/accepted way to center the dialog respecting the current location of the primary shell (the workbench app If I am not mistaken)? I can center the dialog on the primary monitor, but I am unable to center the dialog respecting the main app window.
This is a problem on multi-monitor setups. I start the app and it is displayed on the primary monitor. If I move the app to the second screen, I am failing to center the dialog not on the primary monitor but on the second screen where the app is.
---- Update 1 ----
At the moment every dialog is created with a new Shell:
Shell shell = new Shell(Display.getCurrent(), SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
PortOverviewDialog portOverviewDialog = new PortOverviewDialog(shell);
portOverviewDialog.show();
I think this is the main problem. How can I avoid this by getting the primary Shell?
---- Update 2 ----
For me the code Shell shell = HandlerUtil.getActiveShell(event); gets the parent Shell. And I am setting the Dialog to "parent center" with the help of the following code:
Rectangle parentSize = getParent().getBounds();
Rectangle shellSize = shell.getBounds();
int locationX = (parentSize.width - shellSize.width)/2+parentSize.x;
int locationY = (parentSize.height - shellSize.height)/2+parentSize.y;
shell.setLocation(new Point(locationX, locationY));

wxPython window randomly freezes on RPi and acts weird afterwards

This Problem occurs on the RPi (3B+, Raspbian Buster) only. I run the program on my Mac without any problems.
Short description of my program:
After entering the mainloop, the program enters a second thread with another loop (call it requestloop) when the designated button is pressed. The requestloop can be left by pressing the button again. This requestloop requests a xml table via url every 10 seconds which is then parsed with ElementTree, sorted and displayed in a wx.grid.Grid. I use grid.ForceRefresh to make sure the grid is updated.
I hope the following snippet helps to understand the above:
def on_btnrun(self, event):
global run
if self.btnrun.Label == "Start":
run = True
thrupt = threading.Thread(target=self.thrupdate)
thrupt.start()
self.btnrun.SetLabel("Ende")
elif self.btnrun.Label == "Ende":
run = False
self.btnrun.SetLabel("Start")
def thrupdate(self):
while run is True:
reset()
self.grid.ClearGrid()
update(self.grid)
self.grid.ForceRefresh()
time.sleep(10)
Problem:
Now as mentioned in the title the whole wx Window freezes after passing the requestloop between roughly 5 and 20 times. This happens completely randomly, I could not find any regularities. The program keeps running though, for it still prints the output in the terminal every cycle (I added this function for testing).
When I now open another window (eg. menu dropwdown) which lays over the wx Window it will be copied onto the wx Window and stay there after I closed it.
Here are some Images to better understand what I mean (ignore all other widgets that I didn't mention, they are just nonfunctional placeholders).
Image of the wx Window before it freezes
Image of the wx Window after it freezes
Image of the wx Window after opening and closing the dropdown menu
Extra-Info: while building wxPython on the RPi I got some warnings and everytime I run the program I get the following one (it says the actual time instead of time):
(program.py:1666): Gtk-Critical **: time: gtk_distribute_natural_allocation: assertion ‚extra_space >= 0‘ failed
Question:
I have no idea why any of this happens. Is wxPython not stable on Raspbian? Or did the build partly fail? Or is the RPi not having enough rendering capacity?
Solved it by using wx.CallAfter in the details of the update() method.

Cycling through workspaces in multi monitor setup in XMonad

I'm currently using alt + ctrl + left and alt + ctrl + right to cycle between workspaces:
...
, ((altModMask .|. controlMask, xK_Left),
prevWS)
, ((altModMask .|. controlMask, xK_Right),
nextWS)
This works fine for single monitor setup. However it is a bit confusing when using a dual monitor setup. This is because the workspace to be shown will change screen if currently visible on another screen. For instance if I have ws 1 on screen 0 and ws 2 on screen 1 and have the focus on screen 0:
1:term (2:web) 3:txt
When I now do a nextWS, ws 2, that was currently on screen 1 will be drawn to screen 0, while screen 1 will show ws 1.
(1:term) 2:web 3:txt
What I would like is a behaviour where prexWS and nextWS will jump over the workspace that is currently shown on the other monitor and only select a workspace that is currently not shown.
Are there such commands already or is there some xmonad.hs example that implements this?
Use XMonad.Actions.DynamicWorkspaceOrder from the xmonad-contrib:
import qualified XMonad.Actions.DynamicWorkspaceOrder as DO
...
, ((altModMask .|. controlMask, xK_Left),
DO.moveTo Prev HiddenNonEmptyWS)
, ((altModMask .|. controlMask, xK_Right),
DO.moveTo Next HiddenNonEmptyWS)

Issue with JQuery window resize function calls

I have a resize method in my js file :
$(window).resize(function(){
my_function();
});
In it i have loads of other functions which need to be called when the window is resized (for the responsive part of a website) ,i have not included all these functions as the problem i keep facing is the same in the one i am about to post.
It can't be simpler :
function my_function() {
console.log('1');
$('body').click(function(){
console.log('2');
});
}
my_function();
//when the function is called the console logs 1,when you click on the body ,it logs 2.
Now the problem, when I resize the window (for example : from full window to half in 2s),the resize() fires a couple of times (for example : 10 [every time it's different]).I know this because in the console the number 1 is called/logged 10 times as the window is being resized.Now,after i finished resizing the window, when i click on the body, the console logs the number 2 the same amount of times (10).
Why ? And how can i make it fire the click.function only once ?
Alex.
UPDATE 1 : in addition,every subsequent click on the body logs 2 another 10 times (so if i click 5 times,the console will log the number 2 50 times.

Gtkmm - "Gdk::Window::pointer_grab" troubles

I am programming an FPS (First Person Shooter) game using "Gtkmm" as a window manager and I would like to do the "mouse-look". Therefore, I have to "grab" the mouse pointer to redirect all the mouse motion events to my application window.
There seems to be three overloaded functions to do that job and I have chosen the simplest one for the beginning:
Gdk::GrabStatus Gdk::Window::pointer_grab(bool owner_events, Gdk::EventMask event_mask, guint32 timestamp)
I have tried to put this function to my application but I have had "bad results" so far - it doesn't do what I want it to, it behaves "differently" on "Windows" than on "Linux", etc...
So I will write down what I have done so far, but first, what is my target: "I want to have my application in a window and want to be able to do the mouse-look with a mouse even when I leave the window with the mouse pointer".
So let's get to the function parameters:
-->bool owner_events: when I set it to "true", I got events only when I was inside the window, but (worse) when I set it to "false", I didn't get any events - so I set it to true (the better option :-) )
-->Gdk::EventMask event_mask: there should be those events which I want to catch. For now, I am interested only in mouse motion events, so I put there only "Gdk::POINTER_MOTION_MASK"
-->guint32 timestamp: this I also don't understand but when I put there pure "0", the grab status was OK, thus "GRAB_SUCCESS" (when I tried to set it to 1, 2 or whatever other number, it returned "GRAB_INVALID_TIME" as a grab status) - so I set it to "0"
And now when I run it on Linux, it although grabs the pointer and when I click somewhere outside the window, it doesn't react (thus my window stays always at the top, which is what "I want"), but the problem is that, that the application doesn't catch any events or catches it only when I am inside the window (when I set owner_events to true).
And on Windows it is yet worse: when I click somewhere outside the window, it switches me to the area where I clicked - so this is the same as "without grabbing".
Could someone tell me, what I am doing wrong, or give me a little example on using grabbing in Gtkmm?
For the event_mask, you should include Gdk::ENTER_NOTIFY_MASK and Gdk::BUTTON_RELEASE_MASK so you can ungrab the point when it either reenters the window or when the button is released.
For timestamp, either pass the GdkEvent...::time member, or Gdk::CURRENT_TIME.