Select syntax theme based on system theme, update cosmic-text
This commit is contained in:
parent
cec433085a
commit
6f0994f752
4 changed files with 55 additions and 38 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -851,7 +851,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-text"
|
name = "cosmic-text"
|
||||||
version = "0.10.0"
|
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 = [
|
dependencies = [
|
||||||
"fontdb 0.15.0",
|
"fontdb 0.15.0",
|
||||||
"libm",
|
"libm",
|
||||||
|
|
@ -4227,7 +4227,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "taffy"
|
name = "taffy"
|
||||||
version = "0.3.11"
|
version = "0.3.11"
|
||||||
source = "git+https://github.com/DioxusLabs/taffy#6d7f3cf86a923a02c9d7b67053096a8b3486f15f"
|
source = "git+https://github.com/DioxusLabs/taffy#1876f72bee5e376023eaa518aa7b8a34c769bd1b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
"grid",
|
"grid",
|
||||||
|
|
|
||||||
|
|
@ -662,7 +662,7 @@ impl cosmic::Application for App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
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(
|
tab_column = tab_column.push(
|
||||||
row![
|
row![
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
use cosmic_text::{Attrs, Buffer, Edit, Metrics, SyntaxEditor, ViEditor, Wrap};
|
use cosmic_text::{Attrs, Buffer, Edit, Metrics, SyntaxEditor, ViEditor, Wrap};
|
||||||
use std::{fs, path::PathBuf, sync::Mutex};
|
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] = &[
|
static FONT_SIZES: &'static [Metrics] = &[
|
||||||
Metrics::new(10.0, 14.0), // Caption
|
Metrics::new(10.0, 14.0), // Caption
|
||||||
|
|
@ -27,7 +27,7 @@ impl Tab {
|
||||||
let editor = SyntaxEditor::new(
|
let editor = SyntaxEditor::new(
|
||||||
Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), FONT_SIZES[1 /* Body */]),
|
Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), FONT_SIZES[1 /* Body */]),
|
||||||
&SYNTAX_SYSTEM,
|
&SYNTAX_SYSTEM,
|
||||||
"base16-eighties.dark",
|
text_box::Appearance::dark().syntax_theme,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,16 +15,35 @@ use cosmic::{
|
||||||
widget::{self, tree, Widget},
|
widget::{self, tree, Widget},
|
||||||
Shell,
|
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 std::{cell::Cell, cmp, sync::Mutex, time::Instant};
|
||||||
|
|
||||||
use crate::{FONT_SYSTEM, SWASH_CACHE};
|
use crate::{FONT_SYSTEM, SWASH_CACHE};
|
||||||
|
|
||||||
pub struct Appearance {
|
pub struct Appearance {
|
||||||
background_color: Option<Color>,
|
pub background_color: Option<Color>,
|
||||||
text_color: 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 {
|
pub trait StyleSheet {
|
||||||
|
|
@ -33,34 +52,26 @@ pub trait StyleSheet {
|
||||||
|
|
||||||
impl StyleSheet for Theme {
|
impl StyleSheet for Theme {
|
||||||
fn appearance(&self) -> Appearance {
|
fn appearance(&self) -> Appearance {
|
||||||
match self.theme_type {
|
if self.theme_type.is_dark() {
|
||||||
ThemeType::Dark | ThemeType::HighContrastDark => Appearance {
|
Appearance::dark()
|
||||||
background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)),
|
} else {
|
||||||
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
|
Appearance::light()
|
||||||
},
|
|
||||||
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),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TextBox<'a, Editor> {
|
pub struct TextBox<'a> {
|
||||||
editor: &'a Mutex<Editor>,
|
editor: &'a Mutex<ViEditor<'static>>,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
|
line_numbers: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Editor> TextBox<'a, Editor> {
|
impl<'a> TextBox<'a> {
|
||||||
pub fn new(editor: &'a Mutex<Editor>) -> Self {
|
pub fn new(editor: &'a Mutex<ViEditor<'static>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
editor,
|
editor,
|
||||||
padding: Padding::new(0.0),
|
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)
|
TextBox::new(editor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,20 +137,25 @@ fn draw_rect(
|
||||||
// Alpha blend with current value
|
// Alpha blend with current value
|
||||||
let offset = line_offset + x as usize;
|
let offset = line_offset + x as usize;
|
||||||
let current = buffer[offset];
|
let current = buffer[offset];
|
||||||
let rb = ((n_alpha * (current & 0x00FF00FF)) + (alpha * (color & 0x00FF00FF))) >> 8;
|
if current & 0xFF000000 == 0 {
|
||||||
let ag = (n_alpha * ((current & 0xFF00FF00) >> 8))
|
// Overwrite if buffer empty
|
||||||
+ (alpha * (0x01000000 | ((color & 0x0000FF00) >> 8)));
|
buffer[offset] = color;
|
||||||
buffer[offset] = (rb & 0x00FF00FF) | (ag & 0xFF00FF00);
|
} 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
|
where
|
||||||
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
||||||
Renderer::Theme: StyleSheet,
|
Renderer::Theme: StyleSheet,
|
||||||
Editor: Edit,
|
|
||||||
{
|
{
|
||||||
fn tag(&self) -> tree::Tag {
|
fn tag(&self) -> tree::Tag {
|
||||||
tree::Tag::of::<State>()
|
tree::Tag::of::<State>()
|
||||||
|
|
@ -251,6 +267,9 @@ where
|
||||||
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
||||||
let mut editor = editor.borrow_with(&mut font_system);
|
let mut editor = editor.borrow_with(&mut font_system);
|
||||||
|
|
||||||
|
// Set theme
|
||||||
|
editor.update_theme(appearance.syntax_theme);
|
||||||
|
|
||||||
// Set metrics and size
|
// Set metrics and size
|
||||||
editor.buffer_mut().set_metrics_and_size(
|
editor.buffer_mut().set_metrics_and_size(
|
||||||
//TODO: get from config
|
//TODO: get from config
|
||||||
|
|
@ -470,14 +489,12 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'editor, Editor, Message, Renderer> From<TextBox<'a, Editor>>
|
impl<'a, 'editor, Message, Renderer> From<TextBox<'a>> for Element<'a, Message, Renderer>
|
||||||
for Element<'a, Message, Renderer>
|
|
||||||
where
|
where
|
||||||
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
||||||
Renderer::Theme: StyleSheet,
|
Renderer::Theme: StyleSheet,
|
||||||
Editor: Edit,
|
|
||||||
{
|
{
|
||||||
fn from(text_box: TextBox<'a, Editor>) -> Self {
|
fn from(text_box: TextBox<'a>) -> Self {
|
||||||
Self::new(text_box)
|
Self::new(text_box)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue