Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uuid = [] # Deprecated; this feature was provided by the now unneeded uuid depen
[dependencies]
keyboard-types = { version = "0.6.1", default-features = false }
raw-window-handle = "0.6.2"
dpi = "0.1.2"

[target.'cfg(target_os="linux")'.dependencies]
x11rb = { version = "0.13.2", features = ["cursor", "resource_manager", "allow-unsafe-code", "dl-libxcb"], default-features = false }
Expand Down
89 changes: 41 additions & 48 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use baseview::dpi::LogicalSize;
use baseview::{
Event, EventStatus, PhySize, Window, WindowContext, WindowEvent, WindowHandle, WindowHandler,
WindowOpenOptions,
Event, EventStatus, Window, WindowContext, WindowHandle, WindowHandler, WindowOpenOptions,
WindowSize,
};
use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;

struct ParentWindowHandler {
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
current_size: Cell<PhySize>,
damaged: Cell<bool>,

_child_window: Option<WindowHandle>,
Expand All @@ -17,21 +17,19 @@ impl ParentWindowHandler {
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
let size = window.size().physical;
surface
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
.unwrap();

let window_open_options =
WindowOpenOptions::new().with_size(256.0, 256.0).with_title("baseview child");
let window_open_options = WindowOpenOptions::new()
.with_size(LogicalSize::new(256, 256))
.with_title("baseview child");

let child_window =
Window::open_parented(&window, window_open_options, ChildWindowHandler::new);

// TODO: no way to query physical size initially?
Self {
surface: surface.into(),
current_size: PhySize::new(512, 512).into(),
damaged: true.into(),
_child_window: Some(child_window),
}
Self { surface: surface.into(), damaged: true.into(), _child_window: Some(child_window) }
}
}

Expand All @@ -46,20 +44,19 @@ impl WindowHandler for ParentWindowHandler {
buf.present().unwrap();
}

fn resized(&self, new_size: WindowSize) {
println!("Parent Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}

fn on_event(&self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Parent Resized: {:?}", info);
let new_size = info.physical_size();
self.current_size.set(new_size);

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}
Event::Mouse(e) => println!("Parent Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Parent Keyboard event: {:?}", e),
Event::Window(e) => println!("Parent Window event: {:?}", e),
Expand All @@ -71,22 +68,19 @@ impl WindowHandler for ParentWindowHandler {

struct ChildWindowHandler {
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
current_size: Cell<PhySize>,
damaged: Cell<bool>,
}

impl ChildWindowHandler {
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window).unwrap();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();

// TODO: no way to query physical size initially?
Self {
surface: surface.into(),
current_size: PhySize::new(256, 256).into(),
damaged: true.into(),
}
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
let size = window.size().physical;
surface
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
.unwrap();

Self { surface: surface.into(), damaged: true.into() }
}
}

Expand All @@ -101,20 +95,19 @@ impl WindowHandler for ChildWindowHandler {
buf.present().unwrap();
}

fn resized(&self, new_size: WindowSize) {
println!("Child Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}

fn on_event(&self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Child Resized: {:?}", info);
let new_size = info.physical_size();
self.current_size.set(new_size);

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}
Event::Mouse(e) => println!("Child Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Child Keyboard event: {:?}", e),
Event::Window(e) => println!("Child Window event: {:?}", e),
Expand All @@ -125,7 +118,7 @@ impl WindowHandler for ChildWindowHandler {
}

fn main() {
let window_open_options = WindowOpenOptions::new().with_size(512.0, 512.0);
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));

Window::open_blocking(window_open_options, ParentWindowHandler::new);
}
61 changes: 30 additions & 31 deletions examples/open_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use rtrb::{Consumer, RingBuffer};

#[cfg(target_os = "macos")]
use baseview::copy_to_clipboard;
use baseview::dpi::{LogicalSize, PhysicalPosition};
use baseview::{
Event, EventStatus, MouseEvent, PhyPoint, PhySize, Window, WindowContext, WindowEvent,
WindowHandler, WindowInfo, WindowOpenOptions,
Event, EventStatus, MouseEvent, Window, WindowContext, WindowHandler, WindowOpenOptions,
WindowSize,
};

#[derive(Debug, Clone)]
Expand All @@ -18,25 +19,36 @@ enum Message {

struct OpenWindowExample {
rx: RefCell<Consumer<Message>>,
window_context: WindowContext,

surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
current_size: Cell<WindowInfo>,
mouse_pos: Cell<PhyPoint>,
mouse_pos: Cell<PhysicalPosition<f64>>,
is_cursor_inside: Cell<bool>,
damaged: Cell<bool>,
}

impl WindowHandler for OpenWindowExample {
fn resized(&self, new_size: WindowSize) {
println!("Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}

fn on_frame(&self) {
if !self.damaged.get() {
return;
}

let mut surface = self.surface.borrow_mut();
let mut pixels = surface.buffer_mut().unwrap();
let size = self.current_size.get().physical_size();
let scale_factor = self.current_size.get().scale();
let (width, height) = (size.width, size.height);
let size = self.window_context.size();
let scale_factor = self.window_context.scale_factor();
let (width, height) = (size.physical.width, size.physical.height);

for index in 0..(width * height) {
let x = index % width;
Expand Down Expand Up @@ -75,11 +87,12 @@ impl WindowHandler for OpenWindowExample {

if self.is_cursor_inside.get() {
let rect_size = (25.0 * scale_factor) as i32;
let mouse_pos = self.mouse_pos.get().cast::<i32>();

let rect_x_start = (self.mouse_pos.get().x - rect_size).clamp(0, width as i32) as u32;
let rect_x_end = (self.mouse_pos.get().x + rect_size).clamp(0, width as i32) as u32;
let rect_y_start = (self.mouse_pos.get().y - rect_size).clamp(0, height as i32) as u32;
let rect_y_end = (self.mouse_pos.get().y + rect_size).clamp(0, height as i32) as u32;
let rect_x_start = (mouse_pos.x - rect_size).clamp(0, width as i32) as u32;
let rect_x_end = (mouse_pos.x + rect_size).clamp(0, width as i32) as u32;
let rect_y_start = (mouse_pos.y - rect_size).clamp(0, height as i32) as u32;
let rect_y_end = (mouse_pos.y + rect_size).clamp(0, height as i32) as u32;

for x in rect_x_start..rect_x_end {
for y in rect_y_start..rect_y_end {
Expand All @@ -98,12 +111,11 @@ impl WindowHandler for OpenWindowExample {
}

fn on_event(&self, event: Event) -> EventStatus {
match &event {
match event {
#[cfg(target_os = "macos")]
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard("This is a test!"),
Event::Mouse(MouseEvent::CursorMoved { position, .. }) => {
let phy_pos = position.to_physical(&self.current_size.get());
self.mouse_pos.set(phy_pos);
self.mouse_pos.set(position);
self.damaged.set(true);
}
Event::Mouse(MouseEvent::CursorEntered) => {
Expand All @@ -114,19 +126,6 @@ impl WindowHandler for OpenWindowExample {
self.is_cursor_inside.set(false);
self.damaged.set(true);
}
Event::Window(WindowEvent::Resized(info)) => {
println!("Resized: {:?}", info);
self.current_size.set(*info);

let new_size = info.physical_size();

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}
_ => {}
}

Expand All @@ -137,7 +136,7 @@ impl WindowHandler for OpenWindowExample {
}

fn main() {
let window_open_options = WindowOpenOptions::new().with_size(512.0, 512.0);
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));

let (mut tx, rx) = RingBuffer::new(128);

Expand All @@ -151,14 +150,14 @@ fn main() {

Window::open_blocking(window_open_options, |window| {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();

OpenWindowExample {
window_context: window,
surface: surface.into(),
rx: rx.into(),
current_size: WindowInfo::from_physical_size(PhySize::new(512, 512), 1.0).into(),
mouse_pos: PhyPoint::new(0, 0).into(),
mouse_pos: PhysicalPosition::new(0., 0.).into(),
is_cursor_inside: false.into(),
damaged: true.into(),
}
Expand Down
Loading