1
0
Fork 0

unify api and file structure across platforms; make example platform-independent

This commit is contained in:
Micah Johnston 2020-05-25 21:10:32 -05:00
parent ff5b7fee79
commit 1b33c3c33e
8 changed files with 88 additions and 89 deletions

View file

@ -1,5 +1,3 @@
use std::ptr::null_mut;
fn main() {
let window_open_options = baseview::WindowOpenOptions {
title: "baseview",
@ -8,18 +6,5 @@ fn main() {
parent: baseview::Parent::None,
};
#[cfg(target_os = "macos")] {
baseview::Window::open(window_open_options);
}
#[cfg(target_os = "windows")] {
baseview::create_window(window_open_options);
loop {
if !baseview::handle_msg(null_mut()) {
break;
}
}
}
#[cfg(target_os = "linux")] {
baseview::run(window_open_options);
}
}

View file

@ -4,12 +4,16 @@ use std::ffi::c_void;
mod win;
#[cfg(target_os = "windows")]
pub use win::*;
#[cfg(target_os = "linux")]
mod x11;
#[cfg(target_os = "linux")]
pub use x11::*;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::Window;
pub use macos::*;
pub enum Parent {
None,
@ -25,8 +29,3 @@ pub struct WindowOpenOptions<'a> {
pub parent: Parent,
}
#[cfg(target_os = "linux")]
pub fn run(options: WindowOpenOptions) {
x11::run(options);
}

2
src/macos/mod.rs Normal file
View file

@ -0,0 +1,2 @@
mod window;
pub use window::*;

View file

@ -11,7 +11,7 @@ use crate::WindowOpenOptions;
pub struct Window;
impl Window {
pub fn open(options: WindowOpenOptions) {
pub fn open(options: WindowOpenOptions) -> Self {
unsafe {
let _pool = NSAutoreleasePool::new(nil);
@ -41,6 +41,8 @@ impl Window {
let current_app = NSRunningApplication::currentApplication(nil);
current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
app.run();
Window
}
}
}

View file

@ -1,3 +1,2 @@
mod window;
pub use window::*;

View file

@ -1,5 +1,7 @@
extern crate winapi;
use std::ptr::null_mut;
use self::winapi::_core::mem::MaybeUninit;
use self::winapi::shared::guiddef::GUID;
use self::winapi::shared::minwindef::{LPARAM, LPVOID, LRESULT, UINT, WPARAM};
@ -14,40 +16,10 @@ use self::winapi::um::winuser::{
use crate::WindowOpenOptions;
pub fn handle_msg(_window: HWND) -> bool {
unsafe {
let mut msg: MSG = MaybeUninit::uninit().assume_init();
loop {
if PeekMessageA(&mut msg, 0 as HWND, 0, 0, PM_REMOVE) == 0 {
return true;
}
if msg.message == WM_QUIT {
return false;
}
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
}
pub struct Window;
pub unsafe extern "system" fn wnd_proc(
hwnd: HWND,
msg: UINT,
w_param: WPARAM,
l_param: LPARAM,
) -> LRESULT {
match msg {
WM_DESTROY => {
PostQuitMessage(0);
}
_ => {
return DefWindowProcA(hwnd, msg, w_param, l_param);
}
}
return 0;
}
pub fn create_window(w: WindowOpenOptions) {
impl Window {
pub fn open(options: WindowOpenOptions) -> Self {
unsafe {
// We generate a unique name for the new window class to prevent name collisions
let mut guid: GUID = MaybeUninit::uninit().assume_init();
@ -93,12 +65,54 @@ pub fn create_window(w: WindowOpenOptions) {
CW_USEDEFAULT,
CW_USEDEFAULT,
// todo: check if usize fits into i32
w.width as i32,
w.height as i32,
options.width as i32,
options.height as i32,
0 as HWND,
0 as HMENU,
hinstance,
0 as LPVOID,
);
loop {
if !handle_msg(null_mut()) {
break;
}
}
}
Window
}
}
fn handle_msg(_window: HWND) -> bool {
unsafe {
let mut msg: MSG = MaybeUninit::uninit().assume_init();
loop {
if PeekMessageA(&mut msg, 0 as HWND, 0, 0, PM_REMOVE) == 0 {
return true;
}
if msg.message == WM_QUIT {
return false;
}
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
}
unsafe extern "system" fn wnd_proc(
hwnd: HWND,
msg: UINT,
w_param: WPARAM,
l_param: LPARAM,
) -> LRESULT {
match msg {
WM_DESTROY => {
PostQuitMessage(0);
}
_ => {
return DefWindowProcA(hwnd, msg, w_param, l_param);
}
}
return 0;
}

2
src/x11/mod.rs Normal file
View file

@ -0,0 +1,2 @@
mod window;
pub use window::*;

View file

@ -8,12 +8,12 @@
use crate::Parent;
use crate::WindowOpenOptions;
struct X11Window {
pub struct Window {
xcb_connection: xcb::Connection,
}
impl X11Window {
pub fn run(options: WindowOpenOptions) -> Self {
impl Window {
pub fn open(options: WindowOpenOptions) -> Self {
// Convert the parent to a X11 window ID if we're given one
let parent = match options.parent {
Parent::None => None,
@ -107,10 +107,6 @@ impl X11Window {
}
}
pub fn run(options: WindowOpenOptions) {
X11Window::run(options);
}
// Figure out the DPI scaling by opening a new temporary connection and asking XCB
// TODO: currently returning (96, 96) on my system, even though I have 4k screens. Problem with my setup perhaps?
#[allow(dead_code)]