Skip to content

Themes: GAT & MultiTheme #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 18, 2020
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ repository = "https://github.com/dhardy/kas"
[features]
# Enables usage of unstable Rust features
nightly = []

# Use Generic Associated Types (experimental)
# Currently (Feb 2020) compiler support is poor.
gat = []

# Enables documentation of APIs for toolkits and internal use.
# This API is not intended for use by end-user applications and
# thus is omitted from built documentation by default.
Expand All @@ -21,6 +26,7 @@ internal_doc = []
[dependencies]
log = "0.4"
smallvec = "1.1"
stack_dst = { version = "0.6", features = ["unsize"], optional = true }

[dependencies.kas-macros]
version = "0.2.0"
Expand Down
2 changes: 1 addition & 1 deletion kas-macros/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub(crate) fn derive(
fn draw(
&self,
draw_handle: &mut dyn kas::theme::DrawHandle,
mgr: &kas::event::Manager
mgr: &kas::event::ManagerState
) {
use kas::{geom::Coord, WidgetCore};
if #is_frame {
Expand Down
6 changes: 6 additions & 0 deletions kas-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ repository = "https://github.com/dhardy/kas"
[features]
default = ["clipboard", "font-kit"]

# Use Generic Associated Types (experimental)
gat = ["kas/gat"]

# Use stack_dst crate for sized unsized types
stack_dst = ["kas/stack_dst"]

[dependencies]
kas = { path = "..", version = "0.2.0", features = ["winit"] }
font-kit = { version = "0.4.0", optional = true }
Expand Down
19 changes: 17 additions & 2 deletions kas-wgpu/examples/custom-theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

//! Custom theme demo
#![feature(proc_macro_hygiene)]
#![cfg_attr(feature = "gat", feature(generic_associated_types))]

use std::cell::Cell;

Expand Down Expand Up @@ -51,7 +52,11 @@ thread_local! {

impl Theme<DrawPipe> for CustomTheme {
type Window = <ShadedTheme as Theme<DrawPipe>>::Window;

#[cfg(not(feature = "gat"))]
type DrawHandle = <ShadedTheme as Theme<DrawPipe>>::DrawHandle;
#[cfg(feature = "gat")]
type DrawHandle<'a> = <ShadedTheme as Theme<DrawPipe>>::DrawHandle<'a>;

fn new_window(&self, draw: &mut DrawPipe, dpi_factor: f32) -> Self::Window {
Theme::<DrawPipe>::new_window(&self.inner, draw, dpi_factor)
Expand All @@ -61,13 +66,23 @@ impl Theme<DrawPipe> for CustomTheme {
Theme::<DrawPipe>::update_window(&self.inner, window, dpi_factor);
}

#[cfg(not(feature = "gat"))]
unsafe fn draw_handle(
&self,
draw: &mut DrawPipe,
theme_window: &mut Self::Window,
window: &mut Self::Window,
rect: Rect,
) -> Self::DrawHandle {
self.inner.draw_handle(draw, theme_window, rect)
self.inner.draw_handle(draw, window, rect)
}
#[cfg(feature = "gat")]
fn draw_handle<'a>(
&'a self,
draw: &'a mut DrawPipe,
window: &'a mut Self::Window,
rect: Rect,
) -> Self::DrawHandle<'a> {
self.inner.draw_handle(draw, window, rect)
}

fn get_fonts<'a>(&self) -> Vec<Font<'a>> {
Expand Down
12 changes: 11 additions & 1 deletion kas-wgpu/examples/gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ fn main() -> Result<(), kas_wgpu::Error> {
-> VoidResponse
{
println!("Theme: {:?}", name);
#[cfg(not(feature = "stack_dst"))]
println!("Warning: switching themes requires feature 'stack_dst'");

mgr.adjust_theme(|theme| theme.set_theme(name));
VoidResponse::None
}
Expand Down Expand Up @@ -132,7 +135,14 @@ fn main() -> Result<(), kas_wgpu::Error> {
},
);

let theme = kas_wgpu::theme::MultiTheme::new();
#[cfg(feature = "stack_dst")]
let theme = kas::theme::MultiTheme::builder()
.add("shaded", kas_wgpu::theme::ShadedTheme::new())
.add("flat", kas_wgpu::theme::FlatTheme::new())
.build();
#[cfg(not(feature = "stack_dst"))]
let theme = kas_wgpu::theme::ShadedTheme::new();

let mut toolkit = kas_wgpu::Toolkit::new(theme)?;
toolkit.add(window)?;
toolkit.run()
Expand Down
2 changes: 2 additions & 0 deletions kas-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

//! Toolkit for kas

#![cfg_attr(feature = "gat", feature(generic_associated_types))]

pub mod draw;
mod event_loop;
mod font;
Expand Down
8 changes: 8 additions & 0 deletions kas-wgpu/src/theme/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,21 @@ impl DimensionsWindow {
}

impl theme::Window<DrawPipe> for DimensionsWindow {
#[cfg(not(feature = "gat"))]
type SizeHandle = SizeHandle<'static>;
#[cfg(feature = "gat")]
type SizeHandle<'a> = SizeHandle<'a>;

#[cfg(not(feature = "gat"))]
unsafe fn size_handle<'a>(&'a mut self, draw: &'a mut DrawPipe) -> Self::SizeHandle {
// We extend lifetimes (unsafe) due to the lack of associated type generics.
let handle = SizeHandle::new(draw, &self.dims);
std::mem::transmute::<SizeHandle<'a>, SizeHandle<'static>>(handle)
}
#[cfg(feature = "gat")]
fn size_handle<'a>(&'a mut self, draw: &'a mut DrawPipe) -> Self::SizeHandle<'a> {
SizeHandle::new(draw, &self.dims)
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
Expand Down
21 changes: 21 additions & 0 deletions kas-wgpu/src/theme/flat_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ pub struct DrawHandle<'a> {

impl theme::Theme<DrawPipe> for FlatTheme {
type Window = DimensionsWindow;

#[cfg(not(feature = "gat"))]
type DrawHandle = DrawHandle<'static>;
#[cfg(feature = "gat")]
type DrawHandle<'a> = DrawHandle<'a>;

fn new_window(&self, _draw: &mut DrawPipe, dpi_factor: f32) -> Self::Window {
DimensionsWindow::new(DIMS, self.font_size, dpi_factor)
Expand All @@ -66,6 +70,7 @@ impl theme::Theme<DrawPipe> for FlatTheme {
window.dims = Dimensions::new(DIMS, self.font_size, dpi_factor);
}

#[cfg(not(feature = "gat"))]
unsafe fn draw_handle<'a>(
&'a self,
draw: &'a mut DrawPipe,
Expand All @@ -83,6 +88,22 @@ impl theme::Theme<DrawPipe> for FlatTheme {
pass: 0,
}
}
#[cfg(feature = "gat")]
fn draw_handle<'a>(
&'a self,
draw: &'a mut DrawPipe,
window: &'a mut Self::Window,
rect: Rect,
) -> Self::DrawHandle<'a> {
DrawHandle {
draw,
window,
cols: &self.cols,
rect,
offset: Coord::ZERO,
pass: 0,
}
}

fn get_fonts<'a>(&self) -> Vec<Font<'a>> {
vec![crate::font::get_font()]
Expand Down
2 changes: 0 additions & 2 deletions kas-wgpu/src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

mod dimensions;
mod flat_theme;
mod multi_theme;
mod shaded_theme;

pub(crate) use dimensions::{Dimensions, DimensionsParams, DimensionsWindow};

pub use flat_theme::FlatTheme;
pub use multi_theme::MultiTheme;
pub use shaded_theme::ShadedTheme;
Loading