Select syntax theme based on system theme, update cosmic-text

This commit is contained in:
Jeremy Soller 2023-11-02 12:56:59 -06:00
parent cec433085a
commit 6f0994f752
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
4 changed files with 55 additions and 38 deletions

4
Cargo.lock generated
View file

@ -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",

View file

@ -662,7 +662,7 @@ impl cosmic::Application for App {
}
fn view(&self) -> Element<Message> {
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![

View file

@ -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();

View file

@ -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<Color>,
text_color: Color,
pub background_color: Option<Color>,
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<Editor>,
pub struct TextBox<'a> {
editor: &'a Mutex<ViEditor<'static>>,
padding: Padding,
line_numbers: bool,
}
impl<'a, Editor> TextBox<'a, Editor> {
pub fn new(editor: &'a Mutex<Editor>) -> Self {
impl<'a> TextBox<'a> {
pub fn new(editor: &'a Mutex<ViEditor<'static>>) -> 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<Editor>) -> TextBox<'a, Editor> {
pub fn text_box<'a>(editor: &'a Mutex<ViEditor<'static>>) -> 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<Message, Renderer> for TextBox<'a, Editor>
impl<'a, 'editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit,
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
@ -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<TextBox<'a, Editor>>
for Element<'a, Message, Renderer>
impl<'a, 'editor, Message, Renderer> From<TextBox<'a>> for Element<'a, Message, Renderer>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit,
{
fn from(text_box: TextBox<'a, Editor>) -> Self {
fn from(text_box: TextBox<'a>) -> Self {
Self::new(text_box)
}
}