我有 sfml 窗口。我需要在窗口的屏幕顶部(或底部)保留空间。像这样做 polybar、lemonbar 等。我该怎么做?
在这一步,我只有一个简单的 sfml 窗口:
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
unsigned int bar_x = 1920, bar_y = 20;
RenderWindow window(VideoMode(bar_x, bar_y), "bar", Style::None);
Vector2u bar_size(bar_x, bar_y);
Vector2i bar_position(0,0);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
// Here should be blocking position function...
if (event.type == Event::Closed)
window.close();
if (event.type == Event::Resized)
window.setSize(bar_size);
if (event.type == Event::Resized)
window.setPosition(Vector2i(0,0));
}
window.clear();
// window.draw(shape);
window.display();
}
return 0;
}
最佳答案
我认为没有办法强制 WMs 保留空间,但许多 WMs 支持并尊重 _NET_WM_STRUT_PARTIAL
EWMH property在屏幕边缘预留空间。这也是 lemonbar 和 polybar 所做的。
来自EWMH Spec :
_NET_WM_STRUT_PARTIAL
_NET_WM_STRUT_PARTIAL, left, right, top, bottom, left_start_y, left_end_y, right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x, bottom_end_x,CARDINAL[12]/32
This property MUST be set by the Client if the window is to reserve space at the edge of the screen. The property contains 4 cardinals specifying the width of the reserved area at each border of the screen, and an additional 8 cardinals specifying the beginning and end corresponding to each of the four struts. The order of the values is left, right, top, bottom, left_start_y, left_end_y, right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x, bottom_end_x. All coordinates are root window coordinates. The client MAY change this property at any time, therefore the Window Manager MUST watch for property notify events if the Window Manager uses this property to assign special semantics to the window.
[...]
For example, for a panel-style Client appearing at the bottom of the screen, 50 pixels tall, and occupying the space from 200-600 pixels from the left of the screen edge would set a bottom strut of 50, and set bottom_start_x to 200 and bottom_end_x to 600. [...]
更新:在仔细研究了 SFML 文档之后,我不确定如何通过 SFML 设置 EMWH 属性,或者是否有可能。您需要自己解决这个问题。如果不可能,一种方法是以某种方式从 SFML 获取窗口句柄 (xcb_window_t
) 并使用类似来自 libxcb 的 xcb_change_property
设置 _NET_WM_STRUT_PARTIAL
属性。查看lemonbar source供引用。
更新 2: 我对 Xlib 不是很熟悉,但我经常看 stalonetray源代码供引用。例如,src/wmh.c
文件中的 ewmh_set_window_strut
函数显示了如何设置 _NET_WM_STRUT_PARTIAL
属性:
/* Set data for _NET_WM_STRUT{,_PARTIAL} hints */
int ewmh_set_window_strut(Display *dpy, Window wnd, wm_strut_t wm_strut)
{
Atom prop_strut;
Atom prop_strut_partial;
prop_strut = XInternAtom(dpy, _NET_WM_STRUT, False);
prop_strut_partial = XInternAtom(dpy, _NET_WM_STRUT_PARTIAL, False);
XChangeProperty(dpy, wnd, prop_strut, XA_CARDINAL, 32, PropModeReplace,
(unsigned char *)wm_strut, _NET_WM_STRUT_SZ);
XChangeProperty(dpy, wnd, prop_strut_partial, XA_CARDINAL, 32, PropModeReplace,
(unsigned char *)wm_strut, _NET_WM_STRUT_PARTIAL_SZ);
return x11_ok();
}
关于c++ - 我怎样才能在我的窗口顶部获得空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57740905/