Skip to content

Commit

Permalink
Merge pull request #436 from kas-gui/work3
Browse files Browse the repository at this point in the history
Update deps and enable winit/x11 by default
  • Loading branch information
dhardy committed Jan 24, 2024
2 parents 2f6b417 + a00c8f3 commit 87b6615
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 99 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rustdoc-args = ["--cfg", "doc_cfg"]
# markdown, resvg. Recommended also: clipboard, yaml (or some config format).
minimal = ["wgpu", "winit", "wayland"]
# All recommended features for optimal experience
default = ["minimal", "view", "image", "resvg", "clipboard", "markdown", "shaping", "spawn"]
default = ["minimal", "x11", "view", "image", "resvg", "clipboard", "markdown", "shaping", "spawn"]
# All standard test target features
# NOTE: dynamic is excluded due to linker problems on Windows
stable = ["default", "serde", "toml", "yaml", "json", "ron", "macros_log"]
Expand Down Expand Up @@ -133,7 +133,7 @@ features = ["raster"]

[dev-dependencies]
chrono = "0.4"
env_logger = "0.10"
env_logger = "0.11"
log = "0.4"

[workspace]
Expand Down
6 changes: 3 additions & 3 deletions crates/kas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ spawn = ["dep:async-global-executor"]
unsafe_node = []

[build-dependencies]
cfg_aliases = "0.1.1"
cfg_aliases = "0.2.0"

[dependencies]
log = "0.4"
Expand All @@ -94,7 +94,7 @@ ron = { version = "0.8.0", package = "ron", optional = true }
toml = { version = "0.8.2", package = "toml", optional = true }
num_enum = "0.7.0"
dark-light = { version = "1.0", optional = true }
raw-window-handle = "0.5.0"
raw-window-handle = "0.6.0"
async-global-executor = { version = "2.3.1", optional = true }
cfg-if = "1.0.0"
smol_str = "0.2.0"
Expand All @@ -121,4 +121,4 @@ version = "0.5.0" # used in doc links
version = "0.29.2"
optional = true
default-features = false
features = ["rwh_05"]
features = ["rwh_06"]
4 changes: 2 additions & 2 deletions crates/kas-core/src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use winit::event_loop::{EventLoop, EventLoopBuilder, EventLoopProxy};

pub struct Application<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> {
el: EventLoop<ProxyAction>,
windows: Vec<Box<super::Window<Data, G::Surface, T>>>,
state: AppState<Data, G::Surface, T>,
windows: Vec<Box<super::Window<Data, G, T>>>,
state: AppState<Data, G, T>,
}

impl_scope! {
Expand Down
27 changes: 17 additions & 10 deletions crates/kas-core/src/app/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::draw::DrawSharedImpl;
use crate::draw::{color::Rgba, DrawIface, WindowCommon};
use crate::geom::Size;
use crate::theme::Theme;
use raw_window_handle as raw;
use raw_window_handle as rwh;
use std::time::Instant;
use thiserror::Error;

Expand All @@ -20,6 +20,10 @@ use thiserror::Error;
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum Error {
/// Window-handle error
#[error(transparent)]
Handle(#[from] rwh::HandleError),

/// Failure from the graphics sub-system
#[error("error from graphics sub-system")]
Graphics(Box<dyn std::error::Error + 'static>),
Expand Down Expand Up @@ -181,10 +185,21 @@ pub trait AppGraphicsBuilder {
type Shared: DrawSharedImpl;

/// Window surface
type Surface: WindowSurface<Shared = Self::Shared> + 'static;
type Surface<'a>: WindowSurface<Shared = Self::Shared>;

/// Construct shared state
fn build(self) -> Result<Self::Shared>;

/// Construct a window surface
///
/// It is required to call [`WindowSurface::do_resize`] after this.
fn new_surface<'window, W>(
shared: &mut Self::Shared,
window: W,
) -> Result<Self::Surface<'window>>
where
W: rwh::HasWindowHandle + rwh::HasDisplayHandle + Send + Sync + 'window,
Self: Sized;
}

/// Window graphical surface requirements
Expand All @@ -194,14 +209,6 @@ pub trait WindowSurface {
/// Shared draw state
type Shared: kas::draw::DrawSharedImpl;

/// Construct an instance from a window handle
///
/// It is required to call [`WindowSurface::do_resize`] after this.
fn new<W>(shared: &mut Self::Shared, window: W) -> Result<Self>
where
W: raw::HasRawWindowHandle + raw::HasRawDisplayHandle,
Self: Sized;

/// Get current surface size
fn size(&self) -> Size;

Expand Down
14 changes: 7 additions & 7 deletions crates/kas-core/src/app/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

//! Event loop and handling

use super::{AppData, AppState, Pending};
use super::{ProxyAction, Window, WindowSurface};
use super::{AppData, AppGraphicsBuilder, AppState, Pending};
use super::{ProxyAction, Window};
use kas::theme::Theme;
use kas::{Action, WindowId};
use std::collections::HashMap;
Expand All @@ -16,28 +16,28 @@ use winit::event_loop::{ControlFlow, EventLoopWindowTarget};
use winit::window as ww;

/// Event-loop data structure (i.e. all run-time state)
pub(super) struct Loop<A: AppData, S: WindowSurface, T: Theme<S::Shared>>
pub(super) struct Loop<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>>
where
T::Window: kas::theme::Window,
{
/// State is suspended until we receive Event::Resumed
suspended: bool,
/// Window states
windows: HashMap<WindowId, Box<Window<A, S, T>>>,
windows: HashMap<WindowId, Box<Window<A, G, T>>>,
popups: HashMap<WindowId, WindowId>,
/// Translates our WindowId to winit's
id_map: HashMap<ww::WindowId, WindowId>,
/// Application state passed from Toolkit
state: AppState<A, S, T>,
state: AppState<A, G, T>,
/// Timer resumes: (time, window identifier)
resumes: Vec<(Instant, WindowId)>,
}

impl<A: AppData, S: WindowSurface, T: Theme<S::Shared>> Loop<A, S, T>
impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Loop<A, G, T>
where
T::Window: kas::theme::Window,
{
pub(super) fn new(mut windows: Vec<Box<Window<A, S, T>>>, state: AppState<A, S, T>) -> Self {
pub(super) fn new(mut windows: Vec<Box<Window<A, G, T>>>, state: AppState<A, G, T>) -> Self {
Loop {
suspended: true,
windows: windows.drain(..).map(|w| (w.window_id, w)).collect(),
Expand Down
39 changes: 26 additions & 13 deletions crates/kas-core/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ impl AppData for () {

#[crate::autoimpl(Debug)]
#[cfg(winit)]
enum Pending<A: AppData, S: WindowSurface, T: kas::theme::Theme<S::Shared>> {
enum Pending<A: AppData, G: AppGraphicsBuilder, T: kas::theme::Theme<G::Shared>> {
AddPopup(WindowId, WindowId, kas::PopupDescriptor),
// NOTE: we don't need S, T here if we construct the Window later.
// NOTE: we don't need G, T here if we construct the Window later.
// But this way we can pass a single boxed value.
AddWindow(WindowId, Box<Window<A, S, T>>),
AddWindow(WindowId, Box<Window<A, G, T>>),
CloseWindow(WindowId),
Action(kas::Action),
}
Expand All @@ -71,7 +71,7 @@ enum ProxyAction {
#[cfg(test)]
mod test {
use super::*;
use raw_window_handle as raw;
use raw_window_handle as rwh;
use std::time::Instant;

struct Draw;
Expand Down Expand Up @@ -248,14 +248,6 @@ mod test {
impl WindowSurface for Surface {
type Shared = DrawShared;

fn new<W>(_: &mut Self::Shared, _: W) -> Result<Self>
where
W: raw::HasRawWindowHandle + raw::HasRawDisplayHandle,
Self: Sized,
{
todo!()
}

fn size(&self) -> crate::prelude::Size {
todo!()
}
Expand All @@ -280,10 +272,31 @@ mod test {
}
}

struct AGB;
impl AppGraphicsBuilder for AGB {
type DefaultTheme = crate::theme::SimpleTheme;

type Shared = DrawShared;

type Surface<'a> = Surface;

fn build(self) -> Result<Self::Shared> {
todo!()
}

fn new_surface<'window, W>(_: &mut Self::Shared, _: W) -> Result<Self::Surface<'window>>
where
W: rwh::HasWindowHandle + rwh::HasDisplayHandle + Send + Sync + 'window,
Self: Sized,
{
todo!()
}
}

#[test]
fn size_of_pending() {
assert_eq!(
std::mem::size_of::<Pending<(), Surface, crate::theme::SimpleTheme>>(),
std::mem::size_of::<Pending<(), AGB, crate::theme::SimpleTheme>>(),
32
);
}
Expand Down
22 changes: 11 additions & 11 deletions crates/kas-core/src/app/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

//! Shared state

use super::{AppData, Error, Pending, Platform, WindowSurface};
use super::{AppData, AppGraphicsBuilder, Error, Pending, Platform};
use kas::config::Options;
use kas::draw::DrawShared;
use kas::theme::{Theme, ThemeControl};
Expand All @@ -21,35 +21,35 @@ use std::task::Waker;
#[cfg(feature = "clipboard")] use arboard::Clipboard;

/// Application state used by [`AppShared`]
pub(crate) struct AppSharedState<Data: AppData, S: WindowSurface, T: Theme<S::Shared>> {
pub(crate) struct AppSharedState<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> {
pub(super) platform: Platform,
pub(super) config: Rc<RefCell<kas::event::Config>>,
#[cfg(feature = "clipboard")]
clipboard: Option<Clipboard>,
pub(super) draw: draw::SharedState<S::Shared>,
pub(super) draw: draw::SharedState<G::Shared>,
pub(super) theme: T,
pub(super) pending: VecDeque<Pending<Data, S, T>>,
pub(super) pending: VecDeque<Pending<Data, G, T>>,
pub(super) waker: Waker,
window_id: u32,
}

/// Application state shared by all windows
pub(crate) struct AppState<Data: AppData, S: WindowSurface, T: Theme<S::Shared>> {
pub(super) shared: AppSharedState<Data, S, T>,
pub(crate) struct AppState<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> {
pub(super) shared: AppSharedState<Data, G, T>,
pub(super) data: Data,
/// Estimated scale factor (from last window constructed or available screens)
options: Options,
}

impl<Data: AppData, S: WindowSurface, T: Theme<S::Shared>> AppState<Data, S, T>
impl<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> AppState<Data, G, T>
where
T::Window: kas::theme::Window,
{
/// Construct
pub(super) fn new(
data: Data,
pw: super::PlatformWrapper,
draw_shared: S::Shared,
draw_shared: G::Shared,
mut theme: T,
options: Options,
config: Rc<RefCell<kas::event::Config>>,
Expand Down Expand Up @@ -108,7 +108,7 @@ where
}
}

impl<Data: AppData, S: WindowSurface, T: Theme<S::Shared>> AppSharedState<Data, S, T> {
impl<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> AppSharedState<Data, G, T> {
/// Return the next window identifier
///
/// TODO(opt): this should recycle used identifiers since Id does not
Expand Down Expand Up @@ -207,8 +207,8 @@ pub(crate) trait AppShared {
fn waker(&self) -> &std::task::Waker;
}

impl<Data: AppData, S: WindowSurface, T: Theme<S::Shared>> AppShared
for AppSharedState<Data, S, T>
impl<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> AppShared
for AppSharedState<Data, G, T>
{
fn add_popup(&mut self, parent_id: WindowId, popup: kas::PopupDescriptor) -> WindowId {
let id = self.next_window_id();
Expand Down
Loading

0 comments on commit 87b6615

Please sign in to comment.