Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [No current solution] How to keep toolbar window from disappearing  (Read 296 times)

0 Members and 2 Guests are viewing this topic.

johnnywz00

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
I've messed around with SFML for about 5 years off and on, but this is the first time I've tried to make more than one RenderWindow in a program (a platformer level editor). The second window is just a small floating toolbar. However, because the OS (MacOS 15) views it as a separate system window, it disappears when I click anywhere on the main window (which fills the screen).
I haven't played with this long, but no clean solutions come immediately to mind, so I thought I'd just ask right off.
I *can* store my own boolean about whether the tool window is supposed to be visible, and call `requestFocus` for the tool window on every iteration of the game loop, which brings it back after a click, but: the tool window flashes because it's getting destroyed and recreated on every click in the main window, which looks distracting and inelegant at the least. But also, I haven't even begun to test button clicks or keystroke-activated commands to see whether all the events work properly, so I can't even say that the above approach qualifies as a solution.
« Last Edit: January 22, 2025, 03:21:42 pm by johnnywz00 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11110
    • View Profile
    • development blog
    • Email
Re: How to keep toolbar window from disappearing
« Reply #1 on: January 22, 2025, 10:45:27 am »
SFML doesn't (yet?) provide an always-on-top option. As such, you'll have to dig into OS specific APIs to achieve it.

I don't know anything cocoa, so I don't have a ready made solution for you.
Some web searches might reveal something.
On the SFML side, you can get a native handle of the window, with which you might be able to call specific functions.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

johnnywz00

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: How to keep toolbar window from disappearing
« Reply #2 on: January 22, 2025, 03:20:57 pm »
Thanks for the reply. As I'm only toying with this for myself, I'm probably not going to obsess over it. I will probably just implement my toolbar as a RectangleShape-based entity in the main RenderWindow which can be clicked and dragged around, updating all the tool button locations simultaneously.
As mentioned, I *sort* of managed to keep it on top by calling requestFocus each frame that the isActive boolean is true, but it just looks pretty half baked flashing and reappearing every time a tool clicks the main window.

johnnywz00

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: [No current solution] How to keep toolbar window from disappearing
« Reply #3 on: January 22, 2025, 05:07:34 pm »
For any future readers of this thread, Perplexity spit out this code in regards to using Cocoa to keep a toolbar window on top. I haven't tested it.

-----

Yes, you can use a native window handle from SFML to interact with the Cocoa API and create a second RenderWindow that stays on top of the first, such as a toolbar window. Here's how you can approach this:

1. Obtain the native window handle from SFML:

```cpp
sf::WindowHandle handle = window.getSystemHandle();
```

2. Use Objective-C++ to interact with the Cocoa API. Create a .mm file to mix C++ and Objective-C code:

```objc
#import <Cocoa/Cocoa.h>
#include <SFML/Graphics.hpp>

void createToolbarWindow(sf::WindowHandle mainWindowHandle, sf::RenderWindow& toolbarWindow)
{
    NSWindow* mainWindow = (__bridge NSWindow*)mainWindowHandle;
   
    // Create the toolbar window
    NSRect frame = NSMakeRect(0, 0, 300, 50); // Adjust size as needed
    NSWindow* toolbarNSWindow = [[NSWindow alloc] initWithContentRect:frame
                                                           styleMask:NSWindowStyleMaskBorderless
                                                             backing:NSBackingStoreBuffered
                                                               defer:NO];
   
    [toolbarNSWindow setLevel:NSFloatingWindowLevel];
    [toolbarNSWindow setReleasedWhenClosed:NO];
    [toolbarNSWindow makeKeyAndOrderFront:nil];
   
    // Position the toolbar window relative to the main window
    NSRect mainFrame = [mainWindow frame];
    NSRect toolbarFrame = [toolbarNSWindow frame];
    toolbarFrame.origin.x = mainFrame.origin.x;
    toolbarFrame.origin.y = mainFrame.origin.y + mainFrame.size.height;
    [toolbarNSWindow setFrame:toolbarFrame display:YES];
   
    // Create SFML RenderWindow from the NSWindow
    toolbarWindow.create((__bridge WindowHandle)toolbarNSWindow);
   
    // Set up window following behavior
    [mainWindow addChildWindow:toolbarNSWindow ordered:NSWindowAbove];
}
```

3. In your main C++ file, call the function to create the toolbar window:

```cpp
sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Main Window");
sf::RenderWindow toolbarWindow;

createToolbarWindow(mainWindow.getSystemHandle(), toolbarWindow);

// Your main loop
while (mainWindow.isOpen())
{
    // Handle events and draw to both windows
    // ...
   
    mainWindow.display();
    toolbarWindow.display();
}
```

This approach allows you to create a toolbar window that stays on top of the main SFML window using Cocoa API calls[3][1]. The toolbar window is created as a borderless NSWindow and set to float above other windows. It's then attached to the main window as a child window, ensuring it moves along with the main window.

Remember to compile your project with Objective-C++ support and link against the Cocoa framework[2]. You may need to adjust your build settings or CMake configuration to include the necessary frameworks and compile flags.

Citations:
[1] https://github.com/SFML/SFML/issues/1549
[2] https://www.sfml-dev.org/tutorials/2.6/compile-with-cmake.php
[3] https://stackoverflow.com/questions/525609/use-c-with-cocoa-instead-of-objective-c
[4] https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html
[5] https://en.sfml-dev.org/forums/index.php?topic=1600.15
[6] https://www.sfml-dev.org
[7] https://en.sfml-dev.org/forums/index.php?topic=8247.0
[8] https://www.reddit.com/r/GraphicsProgramming/comments/19cf2ey/how_a_window_drawn_by_glfw_or_sfml_is_different/

 

anything