diff --git a/src/siwin/platforms/any/window.nim b/src/siwin/platforms/any/window.nim index cfd52e5..4f5c56f 100644 --- a/src/siwin/platforms/any/window.nim +++ b/src/siwin/platforms/any/window.nim @@ -97,6 +97,9 @@ type FullscreenChangedEvent* = object of AnyWindowEvent fullscreen*: bool + + MaximizedChangedEvent* = object of AnyWindowEvent + maximized*: bool MouseMoveEvent* = object of AnyWindowEvent pos*: IVec2 @@ -131,7 +134,8 @@ type onWindowMove*: proc(e: WindowMoveEvent) onFocusChanged*: proc(e: FocusChangedEvent) - onFullscreenChanged*: proc(e: FullscreenChangedEvent) + onFullscreenChanged*: proc(e: FullscreenChangedEvent) ## note: sends BEFORE ResizeEvent + onMaximizedChanged*: proc(e: MaximizedChangedEvent) ## note: sends BEFORE ResizeEvent onMouseMove*: proc(e: MouseMoveEvent) onMouseButton*: proc(e: MouseButtonEvent) diff --git a/src/siwin/platforms/winapi/window.nim b/src/siwin/platforms/winapi/window.nim index bada014..73a1a59 100644 --- a/src/siwin/platforms/winapi/window.nim +++ b/src/siwin/platforms/winapi/window.nim @@ -392,7 +392,6 @@ proc releaseAllKeys(window: WindowWinapi) = method `maximized=`*(window: WindowWinapi, v: bool) = - window.m_maximized = v discard ShowWindow(window.handle, if v: SwMaximize else: SwRestore) method `minimized=`*(window: WindowWinapi, v: bool) = @@ -469,7 +468,17 @@ method step*(window: WindowWinapi) = var msg: Msg var catched = false + proc checkStateChanged = + if IsZoomed(window.handle).bool != window.m_maximized: + window.m_maximized = not window.m_maximized + window.eventsHandler.pushEvent onMaximizedChanged, MaximizedChangedEvent(window: window, maximized: window.m_maximized) + + window.m_minimized = IsIconic(window.handle) != 0 + window.m_visible = IsWindowVisible(window.handle) != 0 + window.m_resizable = (GetWindowLongW(window.handle, GwlStyle) and WsThickframe) != 0 + while PeekMessage(msg.addr, 0, 0, 0, PmRemove).bool: + checkStateChanged() catched = true TranslateMessage(msg.addr) DispatchMessage(msg.addr) @@ -480,11 +489,6 @@ method step*(window: WindowWinapi) = if window.m_closed: return - window.m_maximized = IsZoomed(window.handle) != 0 - window.m_minimized = IsIconic(window.handle) != 0 - window.m_visible = IsWindowVisible(window.handle) != 0 - window.m_resizable = (GetWindowLongW(window.handle, GwlStyle) and WsThickframe) != 0 - if not catched: sleep(1) let nows = getTime() diff --git a/src/siwin/platforms/x11/window.nim b/src/siwin/platforms/x11/window.nim index 3605d51..be829e6 100644 --- a/src/siwin/platforms/x11/window.nim +++ b/src/siwin/platforms/x11/window.nim @@ -409,8 +409,6 @@ method `title=`*(window: WindowX11, v: string) = method `fullscreen=`*(window: WindowX11, v: bool) = - if window.m_fullscreen == v: return - var event = window.handle.newClientMessage(atoms.netWmState, [Atom 2, atoms.netWmStateFullscreen]) discard display.XSendEvent( window.handle, 0, SubstructureNotifyMask or SubstructureRedirectMask, event.addr @@ -559,7 +557,6 @@ method drawImage*(window: WindowX11SoftwareRendering, pixels: openarray[ColorBgr method `maximized=`*(window: WindowX11, v: bool) = - window.m_maximized = v if window.fullscreen: window.fullscreen = false var event = window.handle.newClientMessage(atoms.netWmState, [Atom v, atoms.netWmStateMaximizedHorz]) @@ -742,6 +739,7 @@ method step*(window: WindowX11) = var prevEventIsKeyUpRepeated = false proc handleEvent(ev: var XEvent, nextEv: var XEvent, hasNextEvent: bool) = + let repeated = prevEventIsKeyUpRepeated prevEventIsKeyUpRepeated = false case ev.theType @@ -761,6 +759,14 @@ method step*(window: WindowX11) = window.redrawRequested = false # hold on, wait for ConfigureNotify of ConfigureNotify: + let state = (let (kind, data) = window.handle.property(atoms.netWmState, Atom); if kind == XaAtom: data else: @[]) + if atoms.netWmStateFullscreen in state != window.m_fullscreen: + window.m_fullscreen = not window.m_fullscreen + window.eventsHandler.pushEvent onFullscreenChanged, FullscreenChangedEvent(window: window, fullscreen: window.m_fullscreen) + if (atoms.netWmStateMaximizedHorz in state and atoms.netWmStateMaximizedVert in state) != window.m_maximized: + window.m_maximized = not window.m_maximized + window.eventsHandler.pushEvent onMaximizedChanged, MaximizedChangedEvent(window: window, maximized: window.m_maximized) + if ev.xconfigure.width != window.m_size.x or ev.xconfigure.height != window.m_size.y: window.m_size = ivec2(ev.xconfigure.width.int32, ev.xconfigure.height.int32) window.eventsHandler.pushEvent onResize, ResizeEvent(window: window, size: window.m_size, initial: false) @@ -768,11 +774,6 @@ method step*(window: WindowX11) = window.m_pos = ivec2(ev.xconfigure.x.int32, ev.xconfigure.y.int32) window.mouse.pos = cursor().pos - window.m_pos window.eventsHandler.pushEvent onWindowMove, WindowMoveEvent(window: window, pos: window.m_pos) - - let state = (let (kind, data) = window.handle.property(atoms.netWmState, Atom); if kind == XaAtom: data else: @[]) - if atoms.netWmStateFullscreen in state != window.m_fullscreen: - window.m_fullscreen = not window.m_fullscreen - window.eventsHandler.pushEvent onFullscreenChanged, FullscreenChangedEvent(window: window, fullscreen: window.m_fullscreen) if window.syncState == SyncState.syncRecieved: window.syncState = SyncState.syncAndConfigureRecieved @@ -840,7 +841,7 @@ method step*(window: WindowX11) = var key = ev.xkey.extractKey if key != Key.unknown: window.keyboard.pressed.incl key - window.eventsHandler.pushEvent onKey, KeyEvent(window: window, key: key, pressed: true, repeated: prevEventIsKeyUpRepeated) + window.eventsHandler.pushEvent onKey, KeyEvent(window: window, key: key, pressed: true, repeated: repeated) if window.eventsHandler.onTextInput != nil and window.xinContext != nil and (window.keyboard.pressed * {lcontrol, rcontrol, lalt, ralt}).len == 0: var status: Status diff --git a/tests/tests.nim b/tests/tests.nim index ab3d8d6..1009be9 100644 --- a/tests/tests.nim +++ b/tests/tests.nim @@ -79,6 +79,8 @@ test "OpenGL": g = (e.pos.x / e.window.size.x * 2).min(2).max(0) redraw e.window , + onMaximizedChanged: proc(e: MaximizedChangedEvent) = + echo "maximized: ", e.maximized )