Skip to content

Commit 48bc25a

Browse files
authored
Merge pull request #57 from dhardy/work2
Themes: GAT & MultiTheme
2 parents 64836f1 + 4918b16 commit 48bc25a

30 files changed

+1036
-592
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ repository = "https://github.com/dhardy/kas"
1212
[features]
1313
# Enables usage of unstable Rust features
1414
nightly = []
15+
16+
# Use Generic Associated Types (experimental)
17+
# Currently (Feb 2020) compiler support is poor.
18+
gat = []
19+
1520
# Enables documentation of APIs for toolkits and internal use.
1621
# This API is not intended for use by end-user applications and
1722
# thus is omitted from built documentation by default.
@@ -21,6 +26,7 @@ internal_doc = []
2126
[dependencies]
2227
log = "0.4"
2328
smallvec = "1.1"
29+
stack_dst = { version = "0.6", features = ["unsize"], optional = true }
2430

2531
[dependencies.kas-macros]
2632
version = "0.2.0"

kas-macros/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ pub(crate) fn derive(
326326
fn draw(
327327
&self,
328328
draw_handle: &mut dyn kas::theme::DrawHandle,
329-
mgr: &kas::event::Manager
329+
mgr: &kas::event::ManagerState
330330
) {
331331
use kas::{geom::Coord, WidgetCore};
332332
if #is_frame {

kas-wgpu/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ repository = "https://github.com/dhardy/kas"
1212
[features]
1313
default = ["clipboard", "font-kit"]
1414

15+
# Use Generic Associated Types (experimental)
16+
gat = ["kas/gat"]
17+
18+
# Use stack_dst crate for sized unsized types
19+
stack_dst = ["kas/stack_dst"]
20+
1521
[dependencies]
1622
kas = { path = "..", version = "0.2.0", features = ["winit"] }
1723
font-kit = { version = "0.4.0", optional = true }

kas-wgpu/examples/custom-theme.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
//! Custom theme demo
77
#![feature(proc_macro_hygiene)]
8+
#![cfg_attr(feature = "gat", feature(generic_associated_types))]
89

910
use std::cell::Cell;
1011

@@ -51,7 +52,11 @@ thread_local! {
5152

5253
impl Theme<DrawPipe> for CustomTheme {
5354
type Window = <ShadedTheme as Theme<DrawPipe>>::Window;
55+
56+
#[cfg(not(feature = "gat"))]
5457
type DrawHandle = <ShadedTheme as Theme<DrawPipe>>::DrawHandle;
58+
#[cfg(feature = "gat")]
59+
type DrawHandle<'a> = <ShadedTheme as Theme<DrawPipe>>::DrawHandle<'a>;
5560

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

69+
#[cfg(not(feature = "gat"))]
6470
unsafe fn draw_handle(
6571
&self,
6672
draw: &mut DrawPipe,
67-
theme_window: &mut Self::Window,
73+
window: &mut Self::Window,
6874
rect: Rect,
6975
) -> Self::DrawHandle {
70-
self.inner.draw_handle(draw, theme_window, rect)
76+
self.inner.draw_handle(draw, window, rect)
77+
}
78+
#[cfg(feature = "gat")]
79+
fn draw_handle<'a>(
80+
&'a self,
81+
draw: &'a mut DrawPipe,
82+
window: &'a mut Self::Window,
83+
rect: Rect,
84+
) -> Self::DrawHandle<'a> {
85+
self.inner.draw_handle(draw, window, rect)
7186
}
7287

7388
fn get_fonts<'a>(&self) -> Vec<Font<'a>> {

kas-wgpu/examples/gallery.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ fn main() -> Result<(), kas_wgpu::Error> {
9797
-> VoidResponse
9898
{
9999
println!("Theme: {:?}", name);
100+
#[cfg(not(feature = "stack_dst"))]
101+
println!("Warning: switching themes requires feature 'stack_dst'");
102+
100103
mgr.adjust_theme(|theme| theme.set_theme(name));
101104
VoidResponse::None
102105
}
@@ -132,7 +135,14 @@ fn main() -> Result<(), kas_wgpu::Error> {
132135
},
133136
);
134137

135-
let theme = kas_wgpu::theme::MultiTheme::new();
138+
#[cfg(feature = "stack_dst")]
139+
let theme = kas::theme::MultiTheme::builder()
140+
.add("shaded", kas_wgpu::theme::ShadedTheme::new())
141+
.add("flat", kas_wgpu::theme::FlatTheme::new())
142+
.build();
143+
#[cfg(not(feature = "stack_dst"))]
144+
let theme = kas_wgpu::theme::ShadedTheme::new();
145+
136146
let mut toolkit = kas_wgpu::Toolkit::new(theme)?;
137147
toolkit.add(window)?;
138148
toolkit.run()

kas-wgpu/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
//! Toolkit for kas
77
8+
#![cfg_attr(feature = "gat", feature(generic_associated_types))]
9+
810
pub mod draw;
911
mod event_loop;
1012
mod font;

kas-wgpu/src/theme/dimensions.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,21 @@ impl DimensionsWindow {
8181
}
8282

8383
impl theme::Window<DrawPipe> for DimensionsWindow {
84+
#[cfg(not(feature = "gat"))]
8485
type SizeHandle = SizeHandle<'static>;
86+
#[cfg(feature = "gat")]
87+
type SizeHandle<'a> = SizeHandle<'a>;
8588

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

92100
fn as_any_mut(&mut self) -> &mut dyn Any {
93101
self

kas-wgpu/src/theme/flat_theme.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ pub struct DrawHandle<'a> {
5656

5757
impl theme::Theme<DrawPipe> for FlatTheme {
5858
type Window = DimensionsWindow;
59+
60+
#[cfg(not(feature = "gat"))]
5961
type DrawHandle = DrawHandle<'static>;
62+
#[cfg(feature = "gat")]
63+
type DrawHandle<'a> = DrawHandle<'a>;
6064

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

73+
#[cfg(not(feature = "gat"))]
6974
unsafe fn draw_handle<'a>(
7075
&'a self,
7176
draw: &'a mut DrawPipe,
@@ -83,6 +88,22 @@ impl theme::Theme<DrawPipe> for FlatTheme {
8388
pass: 0,
8489
}
8590
}
91+
#[cfg(feature = "gat")]
92+
fn draw_handle<'a>(
93+
&'a self,
94+
draw: &'a mut DrawPipe,
95+
window: &'a mut Self::Window,
96+
rect: Rect,
97+
) -> Self::DrawHandle<'a> {
98+
DrawHandle {
99+
draw,
100+
window,
101+
cols: &self.cols,
102+
rect,
103+
offset: Coord::ZERO,
104+
pass: 0,
105+
}
106+
}
86107

87108
fn get_fonts<'a>(&self) -> Vec<Font<'a>> {
88109
vec![crate::font::get_font()]

kas-wgpu/src/theme/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
88
mod dimensions;
99
mod flat_theme;
10-
mod multi_theme;
1110
mod shaded_theme;
1211

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

1514
pub use flat_theme::FlatTheme;
16-
pub use multi_theme::MultiTheme;
1715
pub use shaded_theme::ShadedTheme;

0 commit comments

Comments
 (0)