From 6f0994f752bade5c307c048abf879a0336095177 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 2 Nov 2023 12:56:59 -0600 Subject: [PATCH] Select syntax theme based on system theme, update cosmic-text --- Cargo.lock | 4 +-- src/main.rs | 2 +- src/tab.rs | 4 +-- src/text_box.rs | 83 +++++++++++++++++++++++++++++-------------------- 4 files changed, 55 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b9e7e1..91a4544 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -851,7 +851,7 @@ dependencies = [ [[package]] name = "cosmic-text" version = "0.10.0" -source = "git+https://github.com/pop-os/cosmic-text?branch=vi-editor#241c4ca357b91334c07cb41e2679857841b33e19" +source = "git+https://github.com/pop-os/cosmic-text?branch=vi-editor#ac389d9eebe1a6d8fe21bae315a853a5e0205b73" dependencies = [ "fontdb 0.15.0", "libm", @@ -4227,7 +4227,7 @@ dependencies = [ [[package]] name = "taffy" version = "0.3.11" -source = "git+https://github.com/DioxusLabs/taffy#6d7f3cf86a923a02c9d7b67053096a8b3486f15f" +source = "git+https://github.com/DioxusLabs/taffy#1876f72bee5e376023eaa518aa7b8a34c769bd1b" dependencies = [ "arrayvec", "grid", diff --git a/src/main.rs b/src/main.rs index facac57..79eeb7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -662,7 +662,7 @@ impl cosmic::Application for App { } fn view(&self) -> Element { - let mut tab_column = widget::column::with_capacity(3).padding([0, 16]); + let mut tab_column = widget::column::with_capacity(3).padding([0, 8]); tab_column = tab_column.push( row![ diff --git a/src/tab.rs b/src/tab.rs index 27133d4..4e98f65 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -3,7 +3,7 @@ use cosmic_text::{Attrs, Buffer, Edit, Metrics, SyntaxEditor, ViEditor, Wrap}; use std::{fs, path::PathBuf, sync::Mutex}; -use crate::{fl, Config, FONT_SYSTEM, SYNTAX_SYSTEM}; +use crate::{fl, text_box, Config, FONT_SYSTEM, SYNTAX_SYSTEM}; static FONT_SIZES: &'static [Metrics] = &[ Metrics::new(10.0, 14.0), // Caption @@ -27,7 +27,7 @@ impl Tab { let editor = SyntaxEditor::new( Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), FONT_SIZES[1 /* Body */]), &SYNTAX_SYSTEM, - "base16-eighties.dark", + text_box::Appearance::dark().syntax_theme, ) .unwrap(); diff --git a/src/text_box.rs b/src/text_box.rs index 8eaad6b..0a44453 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -15,16 +15,35 @@ use cosmic::{ widget::{self, tree, Widget}, Shell, }, - theme::{Theme, ThemeType}, + theme::Theme, }; -use cosmic_text::{Action, Edit, Metrics}; +use cosmic_text::{Action, Edit, Metrics, ViEditor}; use std::{cell::Cell, cmp, sync::Mutex, time::Instant}; use crate::{FONT_SYSTEM, SWASH_CACHE}; pub struct Appearance { - background_color: Option, - text_color: Color, + pub background_color: Option, + pub text_color: Color, + pub syntax_theme: &'static str, +} + +impl Appearance { + pub fn dark() -> Self { + Self { + background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)), + text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF), + syntax_theme: "base16-eighties.dark", + } + } + + pub fn light() -> Self { + Self { + background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)), + text_color: Color::from_rgb8(0x00, 0x00, 0x00), + syntax_theme: "base16-ocean.light", + } + } } pub trait StyleSheet { @@ -33,34 +52,26 @@ pub trait StyleSheet { impl StyleSheet for Theme { fn appearance(&self) -> Appearance { - match self.theme_type { - ThemeType::Dark | ThemeType::HighContrastDark => Appearance { - background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)), - text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF), - }, - ThemeType::Light | ThemeType::HighContrastLight => Appearance { - background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)), - text_color: Color::from_rgb8(0x00, 0x00, 0x00), - }, - //TODO: what to return for these? - _ => Appearance { - background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)), - text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF), - }, + if self.theme_type.is_dark() { + Appearance::dark() + } else { + Appearance::light() } } } -pub struct TextBox<'a, Editor> { - editor: &'a Mutex, +pub struct TextBox<'a> { + editor: &'a Mutex>, padding: Padding, + line_numbers: bool, } -impl<'a, Editor> TextBox<'a, Editor> { - pub fn new(editor: &'a Mutex) -> Self { +impl<'a> TextBox<'a> { + pub fn new(editor: &'a Mutex>) -> Self { Self { editor, padding: Padding::new(0.0), + line_numbers: true, } } @@ -70,7 +81,7 @@ impl<'a, Editor> TextBox<'a, Editor> { } } -pub fn text_box<'a, Editor>(editor: &'a Mutex) -> TextBox<'a, Editor> { +pub fn text_box<'a>(editor: &'a Mutex>) -> TextBox<'a> { TextBox::new(editor) } @@ -126,20 +137,25 @@ fn draw_rect( // Alpha blend with current value let offset = line_offset + x as usize; let current = buffer[offset]; - let rb = ((n_alpha * (current & 0x00FF00FF)) + (alpha * (color & 0x00FF00FF))) >> 8; - let ag = (n_alpha * ((current & 0xFF00FF00) >> 8)) - + (alpha * (0x01000000 | ((color & 0x0000FF00) >> 8))); - buffer[offset] = (rb & 0x00FF00FF) | (ag & 0xFF00FF00); + if current & 0xFF000000 == 0 { + // Overwrite if buffer empty + buffer[offset] = color; + } else { + let rb = + ((n_alpha * (current & 0x00FF00FF)) + (alpha * (color & 0x00FF00FF))) >> 8; + let ag = (n_alpha * ((current & 0xFF00FF00) >> 8)) + + (alpha * (0x01000000 | ((color & 0x0000FF00) >> 8))); + buffer[offset] = (rb & 0x00FF00FF) | (ag & 0xFF00FF00); + } } } } } -impl<'a, 'editor, Editor, Message, Renderer> Widget for TextBox<'a, Editor> +impl<'a, 'editor, Message, Renderer> Widget for TextBox<'a> where Renderer: renderer::Renderer + image::Renderer, Renderer::Theme: StyleSheet, - Editor: Edit, { fn tag(&self) -> tree::Tag { tree::Tag::of::() @@ -251,6 +267,9 @@ where let mut font_system = FONT_SYSTEM.lock().unwrap(); let mut editor = editor.borrow_with(&mut font_system); + // Set theme + editor.update_theme(appearance.syntax_theme); + // Set metrics and size editor.buffer_mut().set_metrics_and_size( //TODO: get from config @@ -470,14 +489,12 @@ where } } -impl<'a, 'editor, Editor, Message, Renderer> From> - for Element<'a, Message, Renderer> +impl<'a, 'editor, Message, Renderer> From> for Element<'a, Message, Renderer> where Renderer: renderer::Renderer + image::Renderer, Renderer::Theme: StyleSheet, - Editor: Edit, { - fn from(text_box: TextBox<'a, Editor>) -> Self { + fn from(text_box: TextBox<'a>) -> Self { Self::new(text_box) } }