Skip to content

Commit

Permalink
Add az_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Feb 14, 2025
1 parent e6ab35d commit bb6fde4
Show file tree
Hide file tree
Showing 5 changed files with 791 additions and 118 deletions.
28 changes: 1 addition & 27 deletions azul-desktop/src/shell/appkit/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,7 @@ use once_cell::sync::Lazy;
use objc2::{declare::ClassDecl, sel, msg_send};

use super::{AppData, MacApp, WindowId};

// We'll store: (tag: i32) => MenuCallback
// (On macOS, `tag` is an `NSInteger` or `i64`. We'll just use `i32` for simplicity.)
pub type CommandMap = BTreeMap<CommandId, MenuCallback>;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MacOsMenuCommands {
pub menu_hash: u64,
pub commands: CommandMap,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MenuTarget {
App,
Window(isize),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct CommandId(pub isize);

impl CommandId {
pub fn new() -> Self {
static NEXT_MENU_TAG: AtomicIsize = AtomicIsize::new(0);
Self(NEXT_MENU_TAG.fetch_add(1, Ordering::SeqCst))
}
}
use crate::shell::{MacOsMenuCommands, MenuTarget, CommandId, CommandMap};

// If the app_data.active_menus[target] differs from the `menu`, creates a new
// NSMenu and returns it. Should only be called on the main thread.
Expand Down
60 changes: 44 additions & 16 deletions azul-desktop/src/shell/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use objc2::runtime::{AnyClass, AnyObject, Class, ClassBuilder, Object, ProtocolO
use objc2::*;
use azul_core::window::{MacOSHandle, MonitorVec, PhysicalSize, ScrollResult, WindowInternal, WindowInternalInit};
use azul_core::window::WindowCreateOptions;
use crate::app::{App, LazyFcCache};
use objc2_core_foundation::CGFloat;
use crate::app::{self, App, LazyFcCache};
use crate::wr_translate::{wr_synchronize_updated_images, AsyncHitTester};
use objc2::runtime::YES;
use objc2::rc::{autoreleasepool, AutoreleasePool, Retained};
Expand All @@ -36,6 +37,7 @@ use objc2_app_kit::NSApplicationActivationPolicy;
use std::{fmt, mem, os::raw::c_char, ptr, rc::Rc};
use self::gl::GlFunctions;
use objc2::rc::Id;
use super::{MenuTarget, CommandMap};
use azul_core::window::{RawWindowHandle, WindowId, WindowsHandle};
use webrender::{
api::{
Expand Down Expand Up @@ -65,7 +67,7 @@ pub(crate) struct GlContextGuard {

pub(crate) struct MacApp {
pub(crate) functions: Rc<GenericGlContext>,
pub(crate) active_menus: BTreeMap<menu::MenuTarget, menu::CommandMap>,
pub(crate) active_menus: BTreeMap<MenuTarget, CommandMap>,
pub(crate) data: Arc<Mutex<AppData>>,
}

Expand Down Expand Up @@ -739,7 +741,6 @@ fn create_observer(
}
}


// Creates a class as a subclass of NSOpenGLView, and registers a "megastruct" ivar, which
// holds a pointer to the NSObject
fn ns_opengl_class() -> ClassBuilder {
Expand Down Expand Up @@ -962,24 +963,51 @@ fn key_up(this: *mut Object, _sel: Sel, event: *mut Object) {
}
}

extern "C"
fn draw_rect(this: *mut AnyObject, _sel: Sel, _dirty_rect: NSRect) {
/// The main "drawRect:" which we override from NSOpenGLView, hooking up the calls
/// to do_resize / rebuild_display_list / gpu_scroll_render depending on the size.
extern "C" fn draw_rect(this: *mut AnyObject, _sel: Sel, dirty_rect: NSRect) {
use crate::shell::event::{do_resize, gpu_scroll_render};
unsafe {
// Retrieve the pointer to our Arc<Mutex<AppData>> from the ivar
let ptr = (*this).get_ivar::<*const c_void>("app");
let ptr = *ptr as *const MacApp;
let ptr = &*ptr;
let mac_app = &*ptr;
let windowid = *(*this).get_ivar::<i64>("windowid");

// REDRAWING: if the width / height of the window differ from the display list w / h,
// relayout the code here (yes, in the redrawing function)

let glc = Window::make_current_gl(msg_send![this, openGLContext]);

const GL_COLOR_BUFFER_BIT: u32 = 0x00004000;
ptr.functions.clear_color(0.0, 1.0, 0.0, 1.0);
ptr.functions.clear(GL_COLOR_BUFFER_BIT);
// Grab the lock to get the Window
if let Ok(mut app_data) = mac_app.data.lock() {
let mut app_data = &mut *app_data;
let w = &mut app_data.windows;
let ud = &mut app_data.userdata;
if let Some(window) = w.get_mut(&WindowId { id: windowid }) {

let bounds: NSRect = msg_send![this, bounds];
let bsf: CGFloat = msg_send![this, backingScaleFactor];
let bdpi = (bsf * 96.0).round() as u32;
let bw = bounds.size.width.round() as u32;
let bh = bounds.size.height.round() as u32;

let stored_size = window.internal.current_window_state.size.dimensions;
let ssw = stored_size.width.round() as u32;
let ssh = stored_size.height.round() as u32;
let sdpi = window.internal.current_window_state.size.dpi;

let glc = Window::make_current_gl(msg_send![this, openGLContext]);

const GL_COLOR_BUFFER_BIT: u32 = 0x00004000;
mac_app.functions.clear_color(1.0, 1.0, 1.0, 1.0);
mac_app.functions.clear(GL_COLOR_BUFFER_BIT);

if bw != ssw || bh != ssh || sdpi != bdpi {
do_resize(window, ud, bw, bh, bdpi, &glc);
window.internal.current_window_state.size.dimensions.width = bw as f32;
window.internal.current_window_state.size.dimensions.height = bh as f32;
window.internal.current_window_state.size.dpi = bdpi as u32;
} else {
gpu_scroll_render(window, ud, &glc);
}

Window::finish_gl(glc);
Window::finish_gl(glc);
}
}
}
}
Loading

0 comments on commit bb6fde4

Please sign in to comment.