Skip to content

Commit

Permalink
add: MaximizedChangedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
levovix0 committed Aug 5, 2023
1 parent b0d8cdb commit 293ab2d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/siwin/platforms/any/window.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ type

FullscreenChangedEvent* = object of AnyWindowEvent
fullscreen*: bool

MaximizedChangedEvent* = object of AnyWindowEvent
maximized*: bool

MouseMoveEvent* = object of AnyWindowEvent
pos*: IVec2
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions src/siwin/platforms/winapi/window.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
19 changes: 10 additions & 9 deletions src/siwin/platforms/x11/window.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -761,18 +759,21 @@ 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)
if ev.xconfigure.x.int != window.m_pos.x or ev.xconfigure.y.int != window.m_pos.y:
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down

0 comments on commit 293ab2d

Please sign in to comment.