vizia_baseview/application.rs
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
use crate::window::create_surface;
use crate::window::ViziaWindow;
use baseview::{Window, WindowHandle, WindowScalePolicy};
use gl_rs as gl;
use gl_rs::types::GLint;
use raw_window_handle::HasRawWindowHandle;
use skia_safe::gpu::gl::FramebufferInfo;
use vizia_core::events::EventManager;
use crate::proxy::queue_get;
use vizia_core::backend::*;
use vizia_core::prelude::*;
#[derive(Debug)]
pub enum ApplicationError {}
///Creating a new application creates a root `Window` and a `Context`. Views declared within the closure passed to `Application::new()` are added to the context and rendered into the root window.
///
/// # Example
/// ```no_run
/// # use vizia_core::prelude::*;
/// # use vizia_baseview::Application;
///
/// Application::new(|cx|{
/// // Content goes here
/// })
/// .run();
///```
/// Calling `run()` on the `Application` causes the program to enter the event loop and for the main window to display.
pub struct Application<F>
where
F: Fn(&mut Context) + Send + 'static,
{
app: F,
window_description: WindowDescription,
window_scale_policy: WindowScalePolicy,
on_idle: Option<Box<dyn Fn(&mut Context) + Send>>,
ignore_default_theme: bool,
}
impl<F> Application<F>
where
F: Fn(&mut Context),
F: 'static + Send,
{
pub fn new(app: F) -> Self {
Self {
app,
window_description: WindowDescription::new(),
window_scale_policy: WindowScalePolicy::SystemScaleFactor,
on_idle: None,
ignore_default_theme: false,
}
}
/// Sets the default built-in theming to be ignored.
pub fn ignore_default_theme(mut self) -> Self {
self.ignore_default_theme = true;
self
}
/// Change the window's scale policy. Not part of [`new()`][Self::new] to keep the same
/// signature as the winit backend. This should only be used for HiDPI scaling, use
/// [`WindowDescription::scale_factor`] to set a separate arbitrary scale factor.
pub fn with_scale_policy(mut self, scale_policy: WindowScalePolicy) -> Self {
self.window_scale_policy = scale_policy;
self
}
pub fn title(mut self, title: &str) -> Self {
self.window_description.title = title.to_owned();
self
}
pub fn inner_size(mut self, size: impl Into<WindowSize>) -> Self {
self.window_description.inner_size = size.into();
self
}
/// A scale factor applied on top of any DPI scaling, defaults to 1.0.
pub fn user_scale_factor(mut self, factor: f64) -> Self {
self.window_description.user_scale_factor = factor;
self
}
/// Open a new window that blocks the current thread until the window is destroyed.
///
/// Do **not** use this in the context of audio plugins, unless it is compiled as a
/// standalone application.
///
/// * `app` - The Vizia application builder.
pub fn run(self) -> Result<(), ApplicationError> {
ViziaWindow::open_blocking(
self.window_description,
self.window_scale_policy,
self.app,
self.on_idle,
self.ignore_default_theme,
);
Ok(())
}
/// Open a new child window.
///
/// This function does **not** block the current thread. This is only to be
/// used in the context of audio plugins.
///
/// * `parent` - The parent window.
/// * `app` - The Vizia application builder.
pub fn open_parented<P: HasRawWindowHandle>(self, parent: &P) -> WindowHandle {
ViziaWindow::open_parented(
parent,
self.window_description,
self.window_scale_policy,
self.app,
self.on_idle,
self.ignore_default_theme,
)
}
/// Takes a closure which will be called at the end of every loop of the application.
///
/// The callback provides a place to run 'idle' processing and happens at the end of each loop but before drawing.
/// If the callback pushes events into the queue in context then the event loop will re-run. Care must be taken not to
/// push events into the queue every time the callback runs unless this is intended.
///
/// # Example
/// ```no_run
/// # use vizia_core::prelude::*;
/// # use vizia_baseview::Application;
/// Application::new(|cx|{
/// // Build application here
/// })
/// .on_idle(|cx|{
/// // Code here runs at the end of every event loop after OS and vizia events have been handled
/// })
/// .run();
/// ```
pub fn on_idle<I: 'static + Fn(&mut Context) + Send>(mut self, callback: I) -> Self {
self.on_idle = Some(Box::new(callback));
self
}
}
pub(crate) struct ApplicationRunner {
cx: BackendContext,
event_manager: EventManager,
pub gr_context: skia_safe::gpu::DirectContext,
should_redraw: bool,
/// If this is set to `true`, then `window_scale_factor` will be updated during
/// [`baseview::WindowEvent::Resized`] events in accordance to the system's reported DPI. This
/// can change at runtime when the window is dragged between displays. Otherwise
/// `window_scale_factor` will not change.
use_system_scaling: bool,
/// The scale factor for the window itself. This is either determined by either the operating
/// system or explicitly overridden by the creator of the window. In some cases window resize
/// events may change this scaling policy. This value is only used when translating logical
/// mouse coordinates to physical window coordinates. For any other use within VIZIA itself this
/// always needs to be multiplied by `user_scale_factor`.
window_scale_factor: f64,
// /// The scale factor applied on top of the `window_scale` to convert the window's logical size
// /// to a physical size. If this is different from `*cx.user_scale_factor` after handling the
// /// events then the window will be resized.
// current_user_scale_factor: f64,
// /// The window's current logical size, before `user_scale_factor` has been applied. Needed to
// /// resize the window when changing the scale factor.
// current_window_size: WindowSize,
pub surface: skia_safe::Surface,
pub dirty_surface: skia_safe::Surface,
}
impl ApplicationRunner {
pub fn new(
cx: BackendContext,
gr_context: skia_safe::gpu::DirectContext,
use_system_scaling: bool,
window_scale_factor: f64,
surface: skia_safe::Surface,
dirty_surface: skia_safe::Surface,
) -> Self {
ApplicationRunner {
should_redraw: true,
gr_context,
event_manager: EventManager::new(),
use_system_scaling,
window_scale_factor,
//current_user_scale_factor: cx.user_scale_factor(),
//current_window_size: *cx.window_size(),
cx,
surface,
dirty_surface,
}
}
/// Handle all reactivity within a frame. The window instance is used to resize the window when
/// needed.
pub fn on_frame_update(&mut self, window: &mut Window) {
while let Some(event) = queue_get() {
self.cx.send_event(event);
}
// Events
self.event_manager.flush_events(self.cx.context(), |window_event| match window_event {
// For some reason calling window.close() crashes baseview on macos
// WindowEvent::WindowClose => *should_close = true,
WindowEvent::FocusIn => {
#[cfg(not(target_os = "linux"))] // not implemented for linux yet
if !window.has_focus() {
window.focus();
}
}
_ => {}
});
// if *cx.window_size() != self.current_window_size
// || cx.user_scale_factor() != self.current_user_scale_factor
// {
// self.current_window_size = *cx.window_size();
// self.current_user_scale_factor = cx.user_scale_factor();
// // The user scale factor is not part of the HiDPI scaling, so baseview should treat it
// // as part of our logical size
// window.resize(baseview::Size {
// width: self.current_window_size.width as f64 * self.current_user_scale_factor,
// height: self.current_window_size.height as f64 * self.current_user_scale_factor,
// });
// // TODO: These calculations are now repeated in three places, should probably be moved
// // to a function
// cx.set_scale_factor(self.window_scale_factor * self.current_user_scale_factor);
// let new_physical_width =
// self.current_window_size.width as f32 * cx.style().scale_factor();
// let new_physical_height =
// self.current_window_size.height as f32 * cx.style().scale_factor();
// cx.set_window_size(new_physical_width, new_physical_height);
// if let Some(surface) = cx.get_surface_mut(Entity::root()) {
// if new_physical_width != 0.0 || new_physical_height != 0.0 {
// let fb_info = {
// let mut fboid: GLint = 0;
// unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
// FramebufferInfo {
// fboid: fboid.try_into().unwrap(),
// format: skia_safe::gpu::gl::Format::RGBA8.into(),
// ..Default::default()
// }
// };
// let backend_render_target = backend_render_targets::make_gl(
// (new_physical_width as i32, new_physical_height as i32),
// None,
// 8,
// fb_info,
// );
// surface.0 = gpu::surfaces::wrap_backend_render_target(
// &mut self.gr_context,
// &backend_render_target,
// SurfaceOrigin::BottomLeft,
// ColorType::RGBA8888,
// None,
// None,
// )
// .expect("Could not create skia surface");
// surface.1 = surface
// .0
// .new_surface_with_dimensions((
// new_physical_width.max(1.0) as i32,
// new_physical_height.max(1.0) as i32,
// ))
// .unwrap();
// }
// }
// cx.needs_refresh();
// // hmmm why are we flushing events again?
// // self.event_manager.flush_events(cx.context());
// }
let context = window.gl_context().expect("Window was created without OpenGL support");
unsafe { context.make_current() };
self.cx.process_style_updates();
unsafe { context.make_not_current() };
self.cx.process_animations();
self.cx.process_visual_updates();
if self.cx.0.windows.iter().any(|(_, window_state)| !window_state.redraw_list.is_empty()) {
self.should_redraw = true;
}
}
pub fn render(&mut self) {
self.cx.draw(Entity::root(), &mut self.surface, &mut self.dirty_surface);
self.gr_context.flush_and_submit();
self.should_redraw = false;
}
pub fn handle_event(&mut self, event: baseview::Event, should_quit: &mut bool) {
if requests_exit(&event) {
self.cx.send_event(Event::new(WindowEvent::WindowClose));
*should_quit = true;
}
let mut update_modifiers = |modifiers: vizia_input::KeyboardModifiers| {
self.cx
.modifiers()
.set(Modifiers::SHIFT, modifiers.contains(vizia_input::KeyboardModifiers::SHIFT));
self.cx
.modifiers()
.set(Modifiers::CTRL, modifiers.contains(vizia_input::KeyboardModifiers::CONTROL));
self.cx
.modifiers()
.set(Modifiers::SUPER, modifiers.contains(vizia_input::KeyboardModifiers::META));
self.cx
.modifiers()
.set(Modifiers::ALT, modifiers.contains(vizia_input::KeyboardModifiers::ALT));
};
match event {
baseview::Event::Mouse(event) => match event {
baseview::MouseEvent::CursorMoved { position, modifiers } => {
update_modifiers(modifiers);
// NOTE: We multiply by `self.window_scale_factor` and not by
// `self.context.style.dpi_factor`. Since the additional scaling by
// internally do additional scaling by `self.context.user_scale_factor` is
// done internally to be able to separate actual HiDPI scaling from
// arbitrary uniform scaling baseview only knows about its own scale
// factor.
let physical_posx = position.x * self.window_scale_factor;
let physical_posy = position.y * self.window_scale_factor;
let cursor_x = (physical_posx) as f32;
let cursor_y = (physical_posy) as f32;
self.cx.emit_origin(WindowEvent::MouseMove(cursor_x, cursor_y));
}
baseview::MouseEvent::ButtonPressed { button, modifiers } => {
update_modifiers(modifiers);
let b = translate_mouse_button(button);
self.cx.emit_origin(WindowEvent::MouseDown(b));
}
baseview::MouseEvent::ButtonReleased { button, modifiers } => {
update_modifiers(modifiers);
let b = translate_mouse_button(button);
self.cx.emit_origin(WindowEvent::MouseUp(b));
}
baseview::MouseEvent::WheelScrolled { delta, modifiers } => {
update_modifiers(modifiers);
let (lines_x, lines_y) = match delta {
baseview::ScrollDelta::Lines { x, y } => (x, y),
baseview::ScrollDelta::Pixels { x, y } => (
if x < 0.0 {
-1.0
} else if x > 1.0 {
1.0
} else {
0.0
},
if y < 0.0 {
-1.0
} else if y > 1.0 {
1.0
} else {
0.0
},
),
};
self.cx.emit_origin(WindowEvent::MouseScroll(lines_x, lines_y));
}
baseview::MouseEvent::CursorEntered => {
self.cx.emit_origin(WindowEvent::MouseEnter);
}
baseview::MouseEvent::CursorLeft => {
self.cx.emit_origin(WindowEvent::MouseLeave);
}
_ => {}
},
baseview::Event::Keyboard(event) => {
let (s, pressed) = match event.state {
vizia_input::KeyState::Down => (MouseButtonState::Pressed, true),
vizia_input::KeyState::Up => (MouseButtonState::Released, false),
};
match event.code {
Code::ShiftLeft | Code::ShiftRight => {
self.cx.modifiers().set(Modifiers::SHIFT, pressed)
}
Code::ControlLeft | Code::ControlRight => {
self.cx.modifiers().set(Modifiers::CTRL, pressed)
}
Code::AltLeft | Code::AltRight => {
self.cx.modifiers().set(Modifiers::ALT, pressed)
}
Code::MetaLeft | Code::MetaRight => {
self.cx.modifiers().set(Modifiers::SUPER, pressed)
}
_ => (),
}
match s {
MouseButtonState::Pressed => {
if let vizia_input::Key::Character(written) = &event.key {
for chr in written.chars() {
self.cx.emit_origin(WindowEvent::CharInput(chr));
}
}
self.cx.emit_origin(WindowEvent::KeyDown(event.code, Some(event.key)));
}
MouseButtonState::Released => {
self.cx.emit_origin(WindowEvent::KeyUp(event.code, Some(event.key)));
}
}
}
baseview::Event::Window(event) => match event {
baseview::WindowEvent::Focused => self.cx.needs_refresh(Entity::root()),
baseview::WindowEvent::Resized(window_info) => {
let fb_info = {
let mut fboid: GLint = 0;
unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: skia_safe::gpu::gl::Format::RGBA8.into(),
..Default::default()
}
};
self.surface = create_surface(
(
window_info.physical_size().width as i32,
window_info.physical_size().height as i32,
),
fb_info,
&mut self.gr_context,
);
self.dirty_surface = self
.surface
.new_surface_with_dimensions((
window_info.physical_size().width as i32,
window_info.physical_size().height as i32,
))
.unwrap();
// // We keep track of the current size before applying the user scale factor while
// // baseview's logical size includes that factor so we need to compensate for it
// self.current_window_size = *self.cx.window_size();
// self.current_window_size.width = (window_info.logical_size().width
// / self.cx.user_scale_factor())
// .round() as u32;
// self.current_window_size.height = (window_info.logical_size().height
// / self.cx.user_scale_factor())
// .round() as u32;
// *self.cx.window_size() = self.current_window_size;
// Only use new DPI settings when `WindowScalePolicy::SystemScaleFactor` was
// used
if self.use_system_scaling {
self.window_scale_factor = window_info.scale();
}
//let user_scale_factor = self.cx.user_scale_factor();
//self.cx.set_scale_factor(self.window_scale_factor * user_scale_factor);
let physical_size =
(window_info.physical_size().width, window_info.physical_size().height);
self.cx.set_window_size(
Entity::root(),
physical_size.0 as f32,
physical_size.1 as f32,
);
self.cx.needs_refresh(Entity::root());
}
baseview::WindowEvent::WillClose => {
self.cx.send_event(Event::new(WindowEvent::WindowClose));
}
_ => {}
},
}
}
pub fn handle_idle(&mut self, on_idle: &Option<Box<dyn Fn(&mut Context) + Send>>) {
if let Some(idle_callback) = on_idle {
self.cx.set_current(Entity::root());
(idle_callback)(self.cx.context());
}
}
}
/// Returns true if the provided event should cause an [`Application`] to
/// exit.
pub fn requests_exit(event: &baseview::Event) -> bool {
match event {
baseview::Event::Window(baseview::WindowEvent::WillClose) => true,
#[cfg(target_os = "macos")]
baseview::Event::Keyboard(event) => {
if event.code == vizia_input::Code::KeyQ
&& event.modifiers == vizia_input::KeyboardModifiers::META
&& event.state == vizia_input::KeyState::Down
{
return true;
}
false
}
_ => false,
}
}
fn translate_mouse_button(button: baseview::MouseButton) -> MouseButton {
match button {
baseview::MouseButton::Left => MouseButton::Left,
baseview::MouseButton::Right => MouseButton::Right,
baseview::MouseButton::Middle => MouseButton::Middle,
baseview::MouseButton::Other(id) => MouseButton::Other(id as u16),
baseview::MouseButton::Back => MouseButton::Other(4),
baseview::MouseButton::Forward => MouseButton::Other(5),
}
}