Skip to content
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

Move contents of kas-theme to kas-core, kas-wgpu #374

Merged
merged 7 commits into from
Jan 16, 2023
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
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ jobs:
run: cargo test --manifest-path crates/kas-widgets/Cargo.toml --all-features
- name: Test kas-resvg
run: cargo test --manifest-path crates/kas-resvg/Cargo.toml --all-features
- name: Test kas-theme
run: cargo test --manifest-path crates/kas-theme/Cargo.toml --all-features
- name: Test kas-wgpu
run: |
cargo test --manifest-path crates/kas-wgpu/Cargo.toml --no-default-features --features raster
Expand Down Expand Up @@ -89,8 +87,6 @@ jobs:
run: cargo test --manifest-path crates/kas-widgets/Cargo.toml
- name: Test kas-resvg
run: cargo test --manifest-path crates/kas-resvg/Cargo.toml --all-features
- name: Test kas-theme
run: cargo test --manifest-path crates/kas-theme/Cargo.toml
- name: Test kas-wgpu
run: cargo test --manifest-path crates/kas-wgpu/Cargo.toml
- name: Test kas-dylib
Expand Down
19 changes: 5 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ default = ["minimal", "view", "yaml", "image", "resvg", "clipboard", "markdown",
#
# Note: only some examples build in this configuration; others need view,
# markdown, resvg. Recommended also: clipboard, yaml (or some config format).
minimal = ["theme", "wgpu"]
minimal = ["wgpu"]

# Enable dynamic linking (faster linking via an extra run-time dependency):
dynamic = ["dep:kas-dylib"]

# Enables usage of unstable Rust features
nightly = ["min_spec", "kas-theme?/nightly", "kas-wgpu?/nightly"]
nightly = ["min_spec"]

# Use min_specialization (enables accelerator underlining for AccelLabel)
min_spec = ["kas-widgets/min_spec"]
Expand All @@ -50,16 +50,14 @@ spec = ["min_spec", "kas-core/spec"]
# Enable view widgets
view = ["dep:kas-view"]

# Enable theme support:
theme = ["dep:kas-theme"]
#Enable WGPU backend:
wgpu = ["dep:kas-wgpu"]

# Enables documentation of APIs for shells and internal usage.
# This API is not intended for use by end-user applications and
# thus is omitted from built documentation by default.
# This flag does not change the API, only built documentation.
internal_doc = ["kas-core/internal_doc", "kas-theme?/internal_doc"]
internal_doc = ["kas-core/internal_doc", "kas-wgpu?/internal_doc"]

# Enables clipboard read/write
clipboard = ["kas-wgpu?/clipboard"]
Expand All @@ -73,7 +71,7 @@ shaping = ["kas-core/shaping"]
harfbuzz = ["kas-core/harfbuzz"]

# Enable config read/write
config = ["kas-core/config", "kas-theme?/config"]
config = ["kas-core/config"]

# Enable support for YAML (de)serialisation
yaml = ["config", "kas-core/yaml"]
Expand All @@ -93,7 +91,7 @@ resvg = ["dep:kas-resvg", "kas-resvg?/svg"]
tiny-skia = ["dep:kas-resvg"]

# Automatically detect usage of dark theme
dark-light = ["kas-theme?/dark-light"]
dark-light = ["kas-core/dark-light"]

# Support SVG images

Expand All @@ -110,12 +108,6 @@ kas-widgets = { version = "0.12.1", path = "crates/kas-widgets" }
kas-view = { version = "0.12.0", path = "crates/kas-view", optional = true }
kas-resvg = { version = "0.12.0", path = "crates/kas-resvg", optional = true }

[dependencies.kas-theme]
version = "0.12.0" # used in doc links
path = "crates/kas-theme"
optional = true
default-features = false

[dependencies.kas-wgpu]
version = "0.12.1"
path = "crates/kas-wgpu"
Expand All @@ -134,7 +126,6 @@ members = [
"crates/kas-dylib",
"crates/kas-macros",
"crates/kas-resvg",
"crates/kas-theme",
"crates/kas-wgpu",
"crates/kas-widgets",
"crates/kas-view",
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ KAS is a pure-Rust GUI toolkit with stateful widgets:
[Troubleshooting](https://github.com/kas-gui/kas/wiki/Troubleshooting)
- API docs: [kas](https://docs.rs/kas), [kas-core](https://docs.rs/kas-core),
[kas-widgets](https://docs.rs/kas-widgets),
[kas-theme](https://docs.rs/kas-theme), [kas-wgpu](https://docs.rs/kas-wgpu)
[kas-wgpu](https://docs.rs/kas-wgpu)
- Prose: [Tutorials](https://kas-gui.github.io/tutorials/),
[Blog](https://kas-gui.github.io/blog/)

Expand Down Expand Up @@ -78,8 +78,7 @@ Crates and features
(`kas-widgets`), etc. [See here](https://kas-gui.github.io/tutorials/#kas).

At this point in time, `kas-wgpu` is the only windowing/rendering implementation
and `kas-theme` the only theme (high-level drawing) implementation, thus `kas`
uses these crates by default, though they are optional.
thus `kas` uses this crate by default, though it is optional.

### Feature flags

Expand Down
4 changes: 4 additions & 0 deletions crates/kas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ winit = ["dep:winit"]
# This is required by config but may be enabled independently
serde = ["dep:serde"]

# Automatically detect usage of dark theme
dark-light = ["dep:dark-light"]

[dependencies]
log = "0.4"
smallvec = "1.6.1"
Expand All @@ -71,6 +74,7 @@ serde_json = { version = "1.0.61", optional = true }
serde_yaml = { version = "0.9.9", optional = true }
ron = { version = "0.8.0", package = "ron", optional = true }
num_enum = "0.5.6"
dark-light = { version = "0.2.2", optional = true }

[dependencies.kas-macros]
version = "0.12.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

//! Animation helpers

use kas::draw::DrawImpl;
use kas::WidgetId;
use crate::draw::DrawImpl;
use crate::WidgetId;
use std::marker::PhantomData;
use std::time::{Duration, Instant};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

//! Colour schemes

use kas::draw::color::{Rgba, Rgba8Srgb};
use kas::event::EventState;
use kas::theme::Background;
use kas::WidgetId;
use crate::draw::color::{Rgba, Rgba8Srgb};
use crate::event::EventState;
use crate::theme::Background;
use crate::WidgetId;
use std::str::FromStr;

const MULT_DEPRESS: f32 = 0.75;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

//! Theme configuration

use crate::{ColorsSrgb, ThemeConfig};
use kas::text::fonts::{fonts, AddMode, FontSelector};
use kas::theme::TextClass;
use kas::TkAction;
use super::{ColorsSrgb, TextClass, ThemeConfig};
use crate::text::fonts::{fonts, AddMode, FontSelector};
use crate::TkAction;
use std::collections::BTreeMap;
use std::time::Duration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use std::any::Any;
use std::f32;
use std::rc::Rc;

use crate::anim::AnimState;
use kas::cast::traits::*;
use kas::dir::Directional;
use kas::geom::{Rect, Size, Vec2};
use kas::layout::{AlignPair, AxisInfo, FrameRules, Margins, SizeRules, Stretch};
use kas::text::{fonts::FontId, TextApi, TextApiExt};
use kas::theme::{Feature, FrameStyle, MarginStyle, MarkStyle, TextClass, ThemeSize};

kas::impl_scope! {
use super::anim::AnimState;
use super::{Config, Feature, FrameStyle, MarginStyle, MarkStyle, TextClass, ThemeSize};
use crate::cast::traits::*;
use crate::dir::Directional;
use crate::geom::{Rect, Size, Vec2};
use crate::layout::{AlignPair, AxisInfo, FrameRules, Margins, SizeRules, Stretch};
use crate::text::{fonts::FontId, TextApi, TextApiExt};

crate::impl_scope! {
/// Parameterisation of [`Dimensions`]
///
/// All dimensions are multiplied by the DPI factor, then rounded to the
Expand Down Expand Up @@ -154,7 +154,7 @@ pub struct Window<D> {
impl<D> Window<D> {
pub fn new(
dims: &Parameters,
config: &crate::Config,
config: &Config,
scale: f32,
fonts: Rc<LinearMap<TextClass, FontId>>,
) -> Self {
Expand All @@ -165,12 +165,12 @@ impl<D> Window<D> {
}
}

pub fn update(&mut self, dims: &Parameters, config: &crate::Config, scale: f32) {
pub fn update(&mut self, dims: &Parameters, config: &Config, scale: f32) {
self.dims = Dimensions::new(dims, config.font_size(), scale);
}
}

impl<D: 'static> crate::Window for Window<D> {
impl<D: 'static> super::Window for Window<D> {
fn size(&self) -> &dyn ThemeSize {
self
}
Expand Down Expand Up @@ -302,7 +302,7 @@ impl<D: 'static> ThemeSize for Window<D> {

fn line_height(&self, class: TextClass) -> i32 {
let font_id = self.fonts.get(&class).cloned().unwrap_or_default();
kas::text::fonts::fonts()
crate::text::fonts::fonts()
.get_first_face(font_id)
.expect("invalid font_id")
.height(self.dims.dpem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ use std::f32;
use std::ops::Range;
use std::time::Instant;

use crate::{dim, ColorsLinear, Config, InputState, SimpleTheme, Theme};
use super::SimpleTheme;
use kas::cast::traits::*;
use kas::dir::{Direction, Directional};
use kas::draw::{color::Rgba, *};
use kas::event::EventState;
use kas::geom::*;
use kas::text::{TextApi, TextDisplay};
use kas::theme::dimensions as dim;
use kas::theme::{Background, FrameStyle, MarkStyle, TextClass};
use kas::theme::{ColorsLinear, Config, InputState, Theme};
use kas::theme::{ThemeControl, ThemeDraw, ThemeSize};
use kas::{TkAction, WidgetId};

Expand All @@ -30,6 +32,9 @@ const SHADOW_HOVER: f32 = 1.1;
const SHADOW_POPUP: f32 = 1.2;

/// A theme with flat (unshaded) rendering
///
/// This is a fully functional theme using only the basic drawing primitives
/// available by default.
#[derive(Clone, Debug)]
pub struct FlatTheme {
base: SimpleTheme,
Expand All @@ -55,7 +60,7 @@ impl FlatTheme {
#[inline]
#[must_use]
pub fn with_font_size(mut self, pt_size: f32) -> Self {
self.base.config.set_font_size(pt_size);
self.base = self.base.with_font_size(pt_size);
self
}

Expand All @@ -65,10 +70,7 @@ impl FlatTheme {
#[inline]
#[must_use]
pub fn with_colours(mut self, scheme: &str) -> Self {
self.base.config.set_active_scheme(scheme);
if let Some(scheme) = self.base.config.get_color_scheme(scheme) {
self.base.cols = scheme.into();
}
self.base = self.base.with_colours(scheme);
self
}
}
Expand Down Expand Up @@ -138,6 +140,15 @@ where
}
}

fn draw_upcast<'a>(
draw: DrawIface<'a, DS>,
ev: &'a mut EventState,
w: &'a mut Self::Window,
cols: &'a ColorsLinear,
) -> Self::Draw<'a> {
DrawHandle { draw, ev, w, cols }
}

fn clear_color(&self) -> Rgba {
self.base.cols.background
}
Expand Down
77 changes: 28 additions & 49 deletions crates/kas-core/src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,38 @@
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0

//! Theme interface

//! Themes
//!
//! Widgets expect the theme to provide an implementation of [`SizeMgr`] and of
//! [`DrawMgr`].
//!
//! Constructing a shell requires a [`Theme`]. Two implementations are provided
//! here: [`SimpleTheme`] and [`FlatTheme`].
//! An adapter, [`MultiTheme`], is also provided.

mod anim;
mod colors;
mod config;
mod draw;
mod flat_theme;
mod multi;
mod simple_theme;
mod size;
mod style;
mod theme_dst;
mod traits;

#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
pub mod dimensions;

pub use colors::{Colors, ColorsLinear, ColorsSrgb, InputState};
pub use config::{Config, RasterConfig};
pub use draw::{Background, DrawMgr, ThemeDraw};
pub use flat_theme::FlatTheme;
pub use multi::{MultiTheme, MultiThemeBuilder};
pub use simple_theme::SimpleTheme;
pub use size::{SizeMgr, ThemeSize};
pub use style::*;

#[allow(unused)] use crate::event::EventMgr;
use crate::TkAction;
use std::ops::{Deref, DerefMut};

/// Interface through which a theme can be adjusted at run-time
///
/// All methods return a [`TkAction`] to enable correct action when a theme
/// is updated via [`EventMgr::adjust_theme`]. When adjusting a theme before
/// the UI is started, this return value can be safely ignored.
pub trait ThemeControl {
/// Set font size
///
/// Units: Points per Em (standard unit of font size)
fn set_font_size(&mut self, pt_size: f32) -> TkAction;

/// Change the colour scheme
///
/// If no scheme by this name is found the scheme is left unchanged.
fn set_scheme(&mut self, scheme: &str) -> TkAction;

/// List available colour schemes
fn list_schemes(&self) -> Vec<&str>;

/// Switch the theme
///
/// Most themes do not react to this method; `kas_theme::MultiTheme` uses
/// it to switch themes.
fn set_theme(&mut self, _theme: &str) -> TkAction {
TkAction::empty()
}
}

impl<T: ThemeControl> ThemeControl for Box<T> {
fn set_font_size(&mut self, size: f32) -> TkAction {
self.deref_mut().set_font_size(size)
}
fn set_scheme(&mut self, scheme: &str) -> TkAction {
self.deref_mut().set_scheme(scheme)
}
fn list_schemes(&self) -> Vec<&str> {
self.deref().list_schemes()
}
fn set_theme(&mut self, theme: &str) -> TkAction {
self.deref_mut().set_theme(theme)
}
}
pub use theme_dst::{MaybeBoxed, ThemeDst};
pub use traits::{Theme, ThemeConfig, ThemeControl, Window};
Loading