-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsurface.rs
More file actions
179 lines (149 loc) · 5.75 KB
/
Copy pathsurface.rs
File metadata and controls
179 lines (149 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use bevy::prelude::Entity;
use bevy::window::{MonitorSelection, WindowLevel, WindowMode};
use processing::prelude::*;
use pyo3::{exceptions::PyRuntimeError, prelude::*};
use crate::glfw::GlfwContext;
use crate::monitor::{self, Monitor};
use crate::set_tracked;
#[pyclass(unsendable)]
pub struct Surface {
pub(crate) entity: Entity,
pub(crate) glfw_ctx: Option<GlfwContext>,
}
#[pymethods]
impl Surface {
pub fn poll_events(&mut self) -> bool {
match &mut self.glfw_ctx {
Some(ctx) => ctx.poll_events(),
None => true, // no-op, offscreen surfaces never close
}
}
#[getter]
pub fn focused(&self) -> PyResult<bool> {
surface_focused(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[getter]
pub fn pixel_density(&self) -> PyResult<f32> {
surface_scale_factor(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[getter]
pub fn pixel_width(&self) -> PyResult<u32> {
surface_physical_width(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[getter]
pub fn pixel_height(&self) -> PyResult<u32> {
surface_physical_height(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[getter]
pub fn display_density(&self) -> PyResult<f32> {
match &self.glfw_ctx {
Some(ctx) => Ok(ctx.content_scale()),
None => Ok(1.0),
}
}
pub fn set_pixel_density(&self, density: f32) -> PyResult<()> {
surface_set_pixel_density(self.entity, density)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn set_title(&self, title: &str) -> PyResult<()> {
surface_set_title(self.entity, title.to_string())
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[getter]
pub fn position(&self) -> PyResult<(i32, i32)> {
let p =
surface_position(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok((p.x, p.y))
}
pub fn set_position(&self, x: i32, y: i32) -> PyResult<()> {
surface_set_position(self.entity, x, y).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn set_visible(&self, visible: bool) -> PyResult<()> {
surface_set_visible(self.entity, visible)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn show(&self) -> PyResult<()> {
self.set_visible(true)
}
pub fn hide(&self) -> PyResult<()> {
self.set_visible(false)
}
pub fn set_resizable(&self, resizable: bool) -> PyResult<()> {
surface_set_resizable(self.entity, resizable)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn set_decorated(&self, decorated: bool) -> PyResult<()> {
surface_set_decorated(self.entity, decorated)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn set_always_on_top(&self, on_top: bool) -> PyResult<()> {
let level = if on_top {
WindowLevel::AlwaysOnTop
} else {
WindowLevel::Normal
};
surface_set_window_level(self.entity, level)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn set_opacity(&self, opacity: f32) -> PyResult<()> {
surface_set_opacity(self.entity, opacity)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn iconify(&self) -> PyResult<()> {
surface_iconify(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn restore(&self) -> PyResult<()> {
surface_restore(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn maximize(&self) -> PyResult<()> {
surface_maximize(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn focus(&self) -> PyResult<()> {
surface_focus(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[pyo3(signature = (monitor=None))]
pub fn set_fullscreen(&self, monitor: Option<&Monitor>) -> PyResult<()> {
let mode = match monitor {
Some(m) => WindowMode::BorderlessFullscreen(MonitorSelection::Entity(m.entity)),
None => WindowMode::Windowed,
};
surface_set_window_mode(self.entity, mode)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn position_on(&self, monitor: &Monitor, x: i32, y: i32) -> PyResult<()> {
surface_position_on_monitor(self.entity, monitor.entity, x, y)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn center_on(&self, monitor: &Monitor) -> PyResult<()> {
surface_center_on_monitor(self.entity, monitor.entity)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
}
impl Drop for Surface {
fn drop(&mut self) {
let _ = surface_destroy(self.entity);
}
}
pub fn sync_globals(
globals: &Bound<'_, PyAny>,
surface: &Surface,
canvas_width: u32,
canvas_height: u32,
) -> PyResult<()> {
set_tracked(globals, "width", canvas_width)?;
set_tracked(globals, "height", canvas_height)?;
set_tracked(globals, "focused", surface.focused()?)?;
set_tracked(globals, "pixel_density", surface.pixel_density()?)?;
set_tracked(globals, "pixel_width", surface.pixel_width()?)?;
set_tracked(globals, "pixel_height", surface.pixel_height()?)?;
let (wx, wy) = surface.position().unwrap_or((0, 0));
set_tracked(globals, "window_x", wx)?;
set_tracked(globals, "window_y", wy)?;
let (dw, dh) = match monitor::primary()? {
Some(m) => (m.width()?, m.height()?),
None => (0, 0),
};
set_tracked(globals, "display_width", dw)?;
set_tracked(globals, "display_height", dh)?;
Ok(())
}