Placing a window near the system tray - cross-platform

I am writing a program that needs to set a window just above/below the traybar for gtk. I have tried using the 2 approaches that failed. One was using the gtk_status_icon_position_menu function and placing the window in the point where the user clicks (in the tray bar). The problem is that these solutions work in gnome(Linux) but not in Windows. In Linux they work because the window manager doesn't seem to allow placement of windows in the tray panel, honoring the closest possible. In Windows this doesn't happen and the window can go "out" of the screen which understandably is not desired.
With this said i went out for a work around. My idea was to set the window in the location of mouse click and get the x and y coordinates of a normal window placement and with it's size check if it would be within the screen boundaries. If it was not make the correction. I have came up with the functions needed but for some reason the gdk_drawable_get_size(window->window ,&WindowWidth, &WindowHeight) and other similar functions only give the correct size value after the second run of the signal function. The result of the first run is just 1 to both size and width. (I have read the issue of X11 not giving correct results, but i think this is not it)
event_button = (GdkEventButton *) event;
if (event_button->button == 1)
{
if (active == 0)
{
gboolean dummy;
gint WindowHeight, WindowWidth, WindowPosition[2];
GdkScreen *screen;
gint ScreenHeight, ScreenWidth;
dummy = FALSE;
gtk_widget_show_all(window);
gtk_window_present(GTK_WINDOW(window));
gtk_status_icon_position_menu(menu, &pos[X], &pos[Y], &dummy, statusicon);
gtk_window_move(GTK_WINDOW(window), pos[X],pos[Y]);
gdk_drawable_get_size(window->window ,&WindowWidth, &WindowHeight);
screen = gtk_status_icon_get_screen(statusicon);
ScreenWidth = gdk_screen_get_width(screen);
ScreenHeight = gdk_screen_get_height(screen);
g_print("Screen: %d, %d\nGeometry: %d, %d\n",ScreenWidth, ScreenHeight, WindowWidth, window->allocation.height);
gtk_entry_set_text(GTK_ENTRY(entry),"");
active = 1;
return TRUE;
}
How can i do what i want in a portable way?

I think gtk_status_icon_position_menu should work fine, as i used it for the same purpose.
Are you trying this stuff with X11, What is the actual result you are getting.

Related

Java FX Dialog "appears" with no size

I'm using JFX 11 on Fedora 33 (GNU/Linux) with the Xorg GUI server (v 1.20.11), creating a dialog thus (kotlin):
fun YNdialog (txt : String) : Boolean {
val dlog = Dialog<ButtonType>()
dlog.dialogPane.apply {
contentText = txt
buttonTypes.apply {
add(ButtonType.OK)
add(ButtonType.CANCEL)
}
minWidth = 200.0
minHeight = 100.0
}
dlog.initOwner(theStage.scene.window)
val r = dlog.showAndWait()
return r.isPresent && r.get() == ButtonType.OK
}
This is called from an EventHandler<ActionEvent> associated with a Button:
if (YNdialog("hello world")) ...
There does seem to be a window created. If I remove the initOwner() call, an icon for it appears in the desktop taskbar (KDE), allowing me to do things like move it, but "maximize" and "resize" are greyed out, and the mouse cursor appears to be dragging nothing, ie., it has no width or height.
With the initOwner() it does much the same thing, except it gets no taskbar icon. Also, there is a visible vertical line one or two pixels thick where it should be (centered over the main window). What's more interesting is a second close button appears in the titlebar of the main window:
This is not part of the javafx interface, but I've never seen the window manager (?) do this before.
Occasionally (maybe one in five or ten times) the dialog does appear, and curiously,
the close button there is offset with space for a second one:
When this happens and I can dismiss the dialog window, the second close button on the main window disappears.
Using Alert
Replacing that function, etc. with:
Alert(Alert.AlertType.CONFIRMATION, "hello world").showAndWait()
.filter { res -> res == ButtonType.OK }
.ifPresent { _ -> log.msg("ok!") }
The result is identical to the YNdialog with initOwner().
Am I missing something here? I got all of this pretty much straight from the docs. I have done custom popup windows (instantiated via FXMLLoader) and have not had any problem with that. Does this experience imply the dialog hierarchy is glitchy in this context and I should roll my own?

Mac OSX: How to detect that NO window is selected?

I am working on the app which resizes the selected window.
It works successfully when any window is selected. But crashes when no window is selected.
Currently fetching front most window from the following code.
AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow);
But how to detect that control is on desktop or all the windows are inactive.
AXUIElementCopyAttributeValue() returns AXError, so you can catch it and then check what happened.
AXError error = AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow);
if (error != kAXErrorSuccess) {
//solve problems here
}
In your specific case there is a value of an error returned: kAXErrorNoValue = -25212
You can use AppleScript to do this. When creating a new project in Xcode, you can choose "Cocoa-AppleScript" as your app type by going under OS X > Other to make an app that has both Obj-C and AppleScript. You can use this code to make the app that has focus do something:
tell current app to ...
You can use this code to change the size of the window
set the bounds of the front window to {x, y, x + width, y + height}
Here, x and y are the distance from the top-left corner.
You can add this to your project and modify the size of the window that is currently at the front. This means that the frontmost window of the app with focus will be resized. This is a working and fully interactive example:
set theWindows to the windows of the current application
if the length of theWindows is 0 then
display alert "There are no windows to resize"
else
display dialog "Enter x" default answer ""
set x to text returned of result as number
display dialog "Enter y" default answer ""
set y to text returned of result as number
display dialog "Enter width" default answer ""
set width to text returned of result as number
set width to (x + width)
display dialog "Enter height" default answer ""
set height to text returned of result as number
set height to (y + height)
tell current application to set the bounds of the front window to {x, y, width, height}
end if
In Yosemite you can create apps that have nothing at their core but AppleScript with the "Script Editor" application. In Mavericks and older systems the app is called "AppleScript Editor". If you need Objective-C for other parts of your app you can create a Cocoa-AppleScript program using the method I described earlier.

Getting Actual Placement of a Flyout

I have a flyout that appears near the cursor when a particular user input is triggered.
According to the docs, the Flyout.Placement property is only a preference and does not necessarily reflect the position of the flyout.
I expect the framework to call GetFlyoutPlacement to determine the actual placement. I want to know what the actual placement is so that I can add a little arrow pointing at the cursor position, something like shown in the following:
Is my only option to make the call to GetFlyoutPlacement myself (triggered on the Flyout.Opening event), or is there some more convenient way to do this?
My calls on GetFlyoutPlacement and GetFlyoutPlacementTargetInfo resulted in Error HRESULT E_FAIL has been returned from a call to a COM component. which didn't leave me very much to go on, so I just ended up implementing what I imagine is the logic behind those functions:
public static FlyoutPlacementMode GetActualFlyoutPlacement(
Rect placementTarget,
Size flyoutSize,
FlyoutPlacementMode preferredPlacement )
{
Rect ViewArea = Window.Current.Bounds;
ViewArea.X = 0; // may have non-zero offset for multi-monitor setups
ViewArea.Y = 0; // but we are only interested in offset relative to app view area
switch (preferredPlacement)
{
case FlyoutPlacementMode.Right:
if (desiredSize.Width < ViewArea.Width - placementTarget.Right) return FlyoutPlacementMode.Right;
if (desiredSize.Width < placementTarget.Left) return FlyoutPlacementMode.Left;
if (desiredSize.Height < placementTarget.Top) return FlyoutPlacementMode.Top;
return FlyoutPlacementMode.Bottom;
case FlyoutPlacementMode.Left:
...
}
}
Checkout the approach I suggested here. I believe it is better, because instead of guessing the internal placement algorithm it just compares the absolute coordinates of the flyout and the target element.

How do I dynamically change the height of a TableViewRow?

Application Type: mobile
Titanium SDK: 3.1.0.GA
Platform & version: iOS 6.1
Device: iOS Simulator
Host Operating System: OSX 10.8.3
Titanium Studio: 3.1.0.201304151600
I'd like to conditionally show/hide a textfield in a TableViewRow. In order to do this I need to expand the height of the row. The following code doesn't work, though. The TableViewRow is actually an Alloy controller. I first tried animating it before I realized that it can't be animated. Now I'm just trying to change the height and that isn't even working. I've tried using the setHeight method along with just setting the height property directly to no avail.
Any ideas?
var notesVisible = false;
function notes_click() {
if(notesVisible == false) {
Ti.API.info('expanding');
$.row.height = 200;
// $.notes_container.setHeight(124);
notesVisible = true;
} else {
Ti.API.info('contracting');
$.row.height = 75;
$.notes_container.setHeight(0);
notesVisible = false;
}
};
There are two good ways of doing this, both should be done from the click event listener.
Method 1) One way is to directly change the "height" variable of the row
Method 2) The second is to create a new row and replace the current row with the new row
Method 1 is more straightforward but I found it to be glitchy depending on what version of the SDK you are using, but with 3.1.0 it should work. Both methods should be called from the 'click' eventListener as its easier to tell Titanium which row to act on based on the click
So here is an example
currentTableview.addEventListener('click',function(e)
{
// DO whatever your row is supposed to do when clicked
// Now lets change the height of the row to a new height, 200 in this example
e.row.height = 200
}
With Method two, it involves creating a new row and then replacing the current row with this call
currentTableview.updateRow(e.index,newlyCreatedUpdatedRow);
I know its an old question asked by some one but the solution provided will not work and i think best solution for this is by making a recursive function and changes the height of your row and need to play with the visibility of views inside that row hopefully will help someone :)

Getting Window Number through OSX Accessibility API

I am working on an application that moves windows of third party applications around on the screen.
To get an overview of all currently open windows, I use
CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
This returns an array of dictionaries defining every open window.
Here's an exemplary dictionary returned:
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 442;
Width = 475;
X = 3123;
Y = "-118";
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 907184;
kCGWindowName = Untitled;
kCGWindowNumber = 7328;
kCGWindowOwnerName = TextEdit;
kCGWindowOwnerPID = 20706;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 3;
},
The dictionary is full of good information used elsewhere but lacks an accessibility object that could be used to modify the windows' positions. Windows are clearly identified by the Window Number.
I am now using the PID (kCGWindowOwnerPID) to create an accessibility object for the window's application:
AXUIElementRef app = AXUIElementCreateApplication(pid);
Followed by retrieving a list of all windows the application has opened using AXUIElementCopyAttributeValues:
NSArray *result;
AXUIElementCopyAttributeValues(
(AXUIElementRef) app,
kAXWindowsAttribute,
0,
99999,
(CFArrayRef *) &result
);
This works and returns an array of AXUIElements.
This is where I am stuck. There seems to be no API call to retrieve the Window Number of an accessibility object. Is there any way to either
a) Find the accessibility object's Window Number (to ultimately iterate over the array and find the right window)
or
b) Otherwise clearly match a window described in the array returned by CGWindowListCopyWindowInfo to the Accessibility Objects returned by AXUIElementCopyAttributeValues?
We ended up hiring a dedicated Accessibility Developer for this task.
It turns out there is no way to do this without using undocumented APIs (a no go in our case).
Luckily, there is a practical workaround:
Loop over all open windows of the app. Get their position, size and title:
AXUIElementCopyAttributeValue(target, kAXPositionAttribute, (CFTypeRef*)&posValue);
AXUIElementCopyAttributeValue(target, kAXSizeAttribute, (CFTypeRef*)&sizeValue);
AXUIElementCopyAttributeValue(target, kAXTitleAttribute, (CFTypeRef*)&titleValue);
Next, convert the position and size into actual CGPoint and CGSize values:
AXValueGetValue(posValue, kAXValueCGPointType, &point);
AXValueGetValue(sizeValue, kAXValueCGSizeType, &size);
Compare the size, position and title against the values returned by the object in CGWindowListCopyWindowInfo().
If they match, you can safely assume it's the window you were looking for and use the already open AXUIElement (target in our case) to work it.
The overhead for looping through all open windows turns out to be negligible on OSX. There is a pretty low cap on how many windows are open at the same time.
Also, while this is not 100% accurate (it is possible that 2 windows have the same position, size and title), we haven't encountered any situation in real usage where this happens so far.
There is a private function for obtaining CG window number for a given AX object for window: _AXUIElementGetWindow .
More details in SO discussion Uniquely identify active window on OS X
It looks like there is no public API to do the task with 100% probability. Identifying windows by title and frame (as described in answer above) will works in 99.9% of cases.
Clarifying other answers that suggest the undocumented call of _AXUIElementGetWindow.
In Swift, do this:
1. Create Bridged-Header.h with the following contents:
#import <AppKit/AppKit.h>
AXError _AXUIElementGetWindow(AXUIElementRef element, uint32_t *identifier);
2. Reference this file in your build settings: (your path may vary, it is relative to project root)
3. Call like this:
// variable 'window' is your AXUIElement window
var cgWindowId = CGWindowID()
if (_AXUIElementGetWindow(window, &gcWindowId) != .success) {.
print("cannot get CGWindow id (objc bridged call)")
}
Congrats, you succesfully called objective C functions via a bridge!