Milky2018/window is a MoonBit windowing library modeled after winit.
It currently targets native macOS (AppKit).
- Supported:
nativetarget on macOS - Not supported yet: Linux, Windows, Web backends
moon add Milky2018/windowYou do not need to manually add AppKit/CoreGraphics link flags in your app; the package provides native link configuration.
import {
"Milky2018/window/core",
"Milky2018/window/macos",
}
struct App {
mut window : @macos.Window?
}
pub impl @macos.ApplicationHandler for App with can_create_surfaces(
self,
event_loop,
) {
let attrs =
@core.WindowAttributes::default()
.with_title("window demo")
let window = event_loop.create_window(attrs)
self.window = Some(window)
window.request_redraw()
}
pub impl @macos.ApplicationHandler for App with window_event(
self,
event_loop,
_id,
event,
) {
for compat_event in event.into_winit_events() {
match compat_event {
@core.WinitWindowEvent::CloseRequested => event_loop.exit()
@core.WinitWindowEvent::Resized(_) =>
match self.window {
Some(window) => window.request_redraw()
None => ()
}
@core.WinitWindowEvent::RedrawRequested => println("redraw requested")
_ => ()
}
}
}
fn main {
let event_loop = @macos.EventLoop::new()
event_loop.run_app({ window: None })
}This library follows MoonBit raise-based error handling (typed errors), not
Result. For example:
EventLoop::try_new()may raise@core.EventLoopErrorWindow::set_cursor_position(...)may raise@core.RequestErrorWindow::request_ime_update(...)may raise@core.ImeRequestError
@Milky2018/window/core: core event/types (WindowEvent,ControlFlow,WindowAttributes, keyboard/mouse/IME data types)@Milky2018/window/macos: macOS runtime API (EventLoop,ActiveEventLoop,Window,EventLoopProxy,ApplicationHandler)@Milky2018/window/dpi: logical/physical size and position types@Milky2018/window: convenience constructors
WindowEvent::into_winit_events() is available when you want a
winit-style compatibility projection.
You can also match native event variants directly:
///|
pub impl @macos.ApplicationHandler for App with window_event(
self,
event_loop,
_id,
event,
) {
match event {
@core.WindowEvent::PointerMoved(_, position, _, _) =>
println("pointer moved: \{position}")
@core.WindowEvent::DragEntered(paths, position) =>
println("drag entered at \{position}: \{paths}")
@core.WindowEvent::CloseRequested => event_loop.exit()
_ => ()
}
}The repository includes runnable examples under examples/*.
moon run examples/window --target native