Harbor

branch main
showing the latest snapshot on main
window_ops.odin 5.3 KB · Plain text
window/examples/window_ops.odin 0644 Raw
package window_ops_example

import "core:fmt"
import win "../window"

decorated := true
maximized := false
fullscreen := false
resizable := true
visible := true

main :: proc() {
    fmt.println("Window Operations Demo")
    fmt.println("======================")

    loop, err := win.create_event_loop()
    if err != .None {
        fmt.eprintln("Failed to create event loop:", err)
        return
    }
    defer win.destroy_event_loop(loop)

    _, win_err := win.create_window(loop, {
        title     = "Window Operations Demo",
        size      = {800, 600},
        resizable = true,
        visible   = true,
        decorated = true,
    })
    if win_err != .None {
        fmt.eprintln("Failed to create window:", win_err)
        return
    }

    fmt.println("\nControls:")
    fmt.println("  D - Toggle decorations")
    fmt.println("  M - Toggle maximize")
    fmt.println("  F - Toggle fullscreen")
    fmt.println("  R - Toggle resizable")
    fmt.println("  H - Hide/show window")
    fmt.println("  1 - Set size to 400x300")
    fmt.println("  2 - Set size to 800x600")
    fmt.println("  3 - Set size to 1200x800")
    fmt.println("  Arrow Keys - Move window")
    fmt.println("  Space - Focus window")
    fmt.println("  I - Minimize window")
    fmt.println("  Escape - Exit")
    fmt.println("")

    win.run(loop, handle_event)

    fmt.println("Goodbye!")
}

handle_event :: proc(event: win.Event, loop: ^win.Event_Loop) -> win.Control_Flow {
    #partial switch e in event {
    case win.Window_Event_Data:
        // Get the first (and only) window
        window: ^win.Window = nil
        if len(loop.windows) > 0 {
            window = loop.windows[0]
        }

        #partial switch we in e.event {
        case win.Close_Requested:
            fmt.println("Exiting...")
            win.exit(loop)

        case win.Key_Input:
            if we.state == .Pressed && !we.repeat && window != nil {
                #partial switch we.key {
                case .D:
                    decorated = !decorated
                    win.set_decorations(window, decorated)
                    fmt.printfln("Decorations: %v", decorated)

                case .M:
                    maximized = !maximized
                    win.set_maximized(window, maximized)
                    fmt.printfln("Maximized: %v", maximized)

                case .F:
                    fullscreen = !fullscreen
                    if fullscreen {
                        win.set_fullscreen(window, win.Fullscreen_Mode{})
                    } else {
                        win.set_fullscreen(window, nil)
                    }
                    fmt.printfln("Fullscreen: %v", fullscreen)

                case .R:
                    resizable = !resizable
                    win.set_resizable(window, resizable)
                    fmt.printfln("Resizable: %v", resizable)

                case .H:
                    visible = !visible
                    win.set_visible(window, visible)
                    fmt.printfln("Visible: %v", visible)

                case .Key_1:
                    win.set_size(window, {400, 300})
                    fmt.println("Set size to 400x300")

                case .Key_2:
                    win.set_size(window, {800, 600})
                    fmt.println("Set size to 800x600")

                case .Key_3:
                    win.set_size(window, {1200, 800})
                    fmt.println("Set size to 1200x800")

                case .Arrow_Up:
                    pos := win.get_position(window)
                    pos.y -= 50
                    win.set_position(window, pos)
                    fmt.printfln("Moved to (%d, %d)", pos.x, pos.y)

                case .Arrow_Down:
                    pos := win.get_position(window)
                    pos.y += 50
                    win.set_position(window, pos)
                    fmt.printfln("Moved to (%d, %d)", pos.x, pos.y)

                case .Arrow_Left:
                    pos := win.get_position(window)
                    pos.x -= 50
                    win.set_position(window, pos)
                    fmt.printfln("Moved to (%d, %d)", pos.x, pos.y)

                case .Arrow_Right:
                    pos := win.get_position(window)
                    pos.x += 50
                    win.set_position(window, pos)
                    fmt.printfln("Moved to (%d, %d)", pos.x, pos.y)

                case .Space:
                    win.focus(window)
                    fmt.println("Focusing window")

                case .I:
                    win.set_minimized(window)
                    fmt.println("Minimizing window")

                case .Escape:
                    fmt.println("Exiting...")
                    win.exit(loop)

                case:
                    // Ignore other keys
                }
            }

        case win.Resized:
            fmt.printfln("Resized to %dx%d", we.size.width, we.size.height)

        case win.Moved:
            fmt.printfln("Moved to (%d, %d)", we.position.x, we.position.y)

        case win.Focused:
            fmt.println("Window focused")

        case win.Unfocused:
            fmt.println("Window unfocused")

        case:
            // Ignore other window events
        }

    case win.Loop_Exiting:
        fmt.println("Event loop exiting")

    case:
        // Ignore other events
    }

    return win.Wait{}
}