Skip to content

Commit

Permalink
Follow up for winit backend and CI (#77)
Browse files Browse the repository at this point in the history
* Fix menu on windows
* Refactor menu initialization
* Fix missing gtk in CI
* Create CONTRIBUTING.MD
* Add info text
  • Loading branch information
matthunz authored Jun 20, 2024
1 parent ccdc31b commit f025d19
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 72 deletions.
33 changes: 13 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,50 +86,40 @@ jobs:
- {
target: x86_64-pc-windows-msvc,
os: windows-latest,
toolchain: "1.75.0",
cross: false,
command: "test",
args: "--all --tests",
setup: ""
}
- {
target: x86_64-apple-darwin,
os: macos-latest,
toolchain: "1.75.0",
cross: false,
command: "test",
args: "--all --tests",
setup: ""
}
- {
target: x86_64-unknown-linux-gnu,
os: ubuntu-latest,
toolchain: "1.75.0",
cross: false,
command: "test",
args: "--all --tests",
}
- {
target: aarch64-apple-ios,
os: macos-latest,
toolchain: "1.75.0",
cross: false,
command: "build",
args: "--package dioxus-mobile",
}
- {
target: aarch64-linux-android,
os: ubuntu-latest,
toolchain: "1.75.0",
cross: true,
command: "build",
args: "--package dioxus-mobile",
setup: "sudo apt-get update; sudo apt-get install --no-install-recommends \
libasound2-dev \
libatk1.0-dev \
libgtk-3-dev \
libudev-dev \
libpango1.0-dev \
libxdo-dev"
}

steps:
- uses: actions/checkout@v4
- name: install stable
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.platform.toolchain }}
toolchain: stable
targets: ${{ matrix.platform.target }}
components: rustfmt

Expand All @@ -151,6 +141,9 @@ jobs:
cache-all-crates: "true"
save-if: ${{ github.ref == 'refs/heads/main' }}

- name: Setup
run: ${{ matrix.platform.setup }}

- name: test
run: |
${{ env.RUST_CARGO_COMMAND }} ${{ matrix.platform.command }} ${{ matrix.platform.args }} --target ${{ matrix.platform.target }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
/Cargo.lock
.DS_Store
experiments
experiments
/.vscode
6 changes: 0 additions & 6 deletions .vscode/settings.json

This file was deleted.

43 changes: 43 additions & 0 deletions CONTRIBUTING.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Contributing to Blitz

Welcome to the Dioxus community!
Blitz is a "native" HTML/CSS renderer built to support the "Dioxus Native" project. It is effectively a lightweight webview except that the JavaScript engine is replaced with a native Rust API which allows Rust reactivity / state management libraries like Dioxus to interface with it directly.

Talk to us in: the #native channel in the [Dioxus Discord](https://discord.gg/BWTrn6d3)

## Development

### Windows
Building Blitz requires Python, which can be installed from the Windows app store.

### Linux
Requirements:
* asound2
* atk1.0
* gtk-3
* udev
* pango1.0
* xdo

For example on Ubuntu you can install these by running:
```sh
sudo apt-get update
sudo apt-get install \
libasound2-dev \
libatk1.0-dev \
libgtk-3-dev \
libudev-dev \
libpango1.0-dev \
libxdo-dev
```

### VSCode
You can add the following JSON to your `.vscode/settings.json` to automically build Blitz on all supported targets.
```json
{
"rust-analyzer.check.features": "all",
"rust-analyzer.cargo.features": "all",
"rust-analyzer.check.allTargets": true,
"rust-analyzer.cargo.allTargets": true
}
```
71 changes: 26 additions & 45 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@ use winit::keyboard::PhysicalKey;
#[allow(unused)]
use wgpu::rwh::HasWindowHandle;

use muda::{AboutMetadata, Menu, MenuId, MenuItem, PredefinedMenuItem, Submenu};
use std::sync::Arc;
use std::task::Waker;
use vello::Scene;
use winit::dpi::LogicalSize;
use winit::event::{ElementState, MouseButton};
use winit::event_loop::{ActiveEventLoop, EventLoopProxy};

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
use winit::platform::unix::WindowExtUnix;
#[cfg(target_os = "windows")]
use winit::platform::windows::WindowExtWindows;
use winit::{event::WindowEvent, keyboard::KeyCode, keyboard::ModifiersState, window::Window};

use muda::{AboutMetadata, Menu, MenuId, MenuItem, PredefinedMenuItem, Submenu};

pub(crate) struct View<'s, Doc: DocumentLike> {
pub(crate) renderer: Renderer<'s, Window, Doc>,
pub(crate) scene: Scene,
pub(crate) waker: Option<Waker>,
/// The state of the keyboard modifiers (ctrl, shift, etc). Winit/Tao don't track these for us so we
/// need to store them in order to have access to them when processing keypress events
keyboard_modifiers: ModifiersState,

/// Main menu bar of this view's window.
menu: Option<Menu>,
}

impl<'a, Doc: DocumentLike> View<'a, Doc> {
Expand All @@ -43,6 +34,7 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
scene: Scene::new(),
waker: None,
keyboard_modifiers: Default::default(),
menu: None,
}
}
}
Expand Down Expand Up @@ -290,14 +282,10 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
}))
.unwrap();

let menu_bar = build_menu();

init_menu_bar(&menu_bar, &window);

// for now, forget the menu_bar so it doesn't get dropped
// there's a bug in muda that causes this to segfault
// todo: we should just store this somewhere
std::mem::forget(menu_bar);
self.menu = Some(init_menu(
#[cfg(target_os = "windows")]
&window,
));

let size: winit::dpi::PhysicalSize<u32> = window.inner_size();
let mut viewport = Viewport::new((size.width, size.height));
Expand All @@ -322,16 +310,25 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
}
}

#[allow(unused)]
pub fn init_menu_bar(menu: &Menu, window: &Window) {
/// Initialize the default menu bar.
pub fn init_menu(#[cfg(target_os = "windows")] window: &Window) -> Menu {
let menu = Menu::new();

// Build the about section
let about = Submenu::new("About", true);
about
.append_items(&[
&PredefinedMenuItem::about("Dioxus".into(), Option::from(AboutMetadata::default())),
&MenuItem::with_id(MenuId::new("dev.show_layout"), "Show layout", true, None),
])
.unwrap();
menu.append(&about).unwrap();

#[cfg(target_os = "windows")]
{
use winit::platform::windows::WindowExtWindows;
use winit::raw_window_handle;
let id = window.window_handle_any_thread().unwrap();
if let raw_window_handle::RawWindowHandle::Win32(rwh) = rwh {
let hwnd = id.hwnd;
_ = menu.init_for_hwnd(hwnd as _);
use winit::raw_window_handle::*;
if let RawWindowHandle::Win32(handle) = window.window_handle().unwrap().as_raw() {
menu.init_for_hwnd(handle.hwnd.get()).unwrap();
}
}

Expand All @@ -348,22 +345,6 @@ pub fn init_menu_bar(menu: &Menu, window: &Window) {
use winit::platform::macos::WindowExtMacOS;
menu.init_for_nsapp();
}
}

fn build_menu() -> Menu {
let menu = Menu::new();

// Build the about section
let about = Submenu::new("About", true);

about
.append_items(&[
&PredefinedMenuItem::about("Dioxus".into(), Option::from(AboutMetadata::default())),
&MenuItem::with_id(MenuId::new("dev.show_layout"), "Show layout", true, None),
])
.unwrap();

menu.append(&about).unwrap();

menu
}

0 comments on commit f025d19

Please sign in to comment.