Shared config with Rc

This commit is contained in:
Adrian Wozniak 2019-01-04 11:46:28 +01:00
parent b092ef65e9
commit c25fe0a4f5
No known key found for this signature in database
GPG Key ID: 3B441F7808FC43C7
10 changed files with 115 additions and 76 deletions

View File

@ -6,33 +6,34 @@ use crate::renderer::Renderer;
use crate::ui::*; use crate::ui::*;
use crate::ui::caret::Caret; use crate::ui::caret::Caret;
use crate::ui::menu_bar::MenuBar; use crate::ui::menu_bar::MenuBar;
use sdl2::rect::Point;
use std::boxed::Box; use std::boxed::Box;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use sdl2::rect::Rect; use sdl2::rect::{Rect, Point};
pub struct AppState { pub struct AppState {
menu_bar: MenuBar, menu_bar: MenuBar,
files: Vec<EditorFile>, files: Vec<EditorFile>,
current_file: usize, current_file: usize,
caret: Caret, caret: Caret,
config: Rc<Config>,
} }
impl AppState { impl AppState {
pub fn new(config: &Config) -> Self { pub fn new(config: Rc<Config>) -> Self {
Self { Self {
menu_bar: MenuBar::new(), menu_bar: MenuBar::new(config.clone()),
files: vec![], files: vec![],
current_file: 0, current_file: 0,
caret: Caret::new(config), caret: Caret::new(config.clone()),
config,
} }
} }
pub fn open_file(&mut self, file_path: String, config: &Config) { pub fn open_file(&mut self, file_path: String) {
use std::fs::read_to_string; use std::fs::read_to_string;
if let Ok(buffer) = read_to_string(&file_path) { if let Ok(buffer) = read_to_string(&file_path) {
let file = EditorFile::new(file_path.clone(), buffer, config); let file = EditorFile::new(file_path.clone(), buffer, self.config.clone());
self.current_file = self.files.len(); self.current_file = self.files.len();
self.files.push(file); self.files.push(file);
}; };
@ -42,7 +43,7 @@ impl AppState {
&mut self.caret &mut self.caret
} }
pub fn delete_front(&mut self, config: &Config) { pub fn delete_front(&mut self) {
let file: &mut EditorFile = if let Some(file) = self.files.get_mut(self.current_file) { let file: &mut EditorFile = if let Some(file) = self.files.get_mut(self.current_file) {
file file
} else { } else {
@ -69,12 +70,12 @@ impl AppState {
let new_file = EditorFile::new( let new_file = EditorFile::new(
file.path(), file.path(),
buffer, buffer,
config, self.config.clone(),
); );
self.files[self.current_file] = new_file; self.files[self.current_file] = new_file;
} }
pub fn delete_back(&mut self, config: &Config) { pub fn delete_back(&mut self) {
let file: &mut EditorFile = if let Some(file) = self.files.get_mut(self.current_file) { let file: &mut EditorFile = if let Some(file) = self.files.get_mut(self.current_file) {
file file
} else { } else {
@ -90,7 +91,7 @@ impl AppState {
let new_file = EditorFile::new( let new_file = EditorFile::new(
file.path(), file.path(),
buffer, buffer,
config, self.config.clone(),
); );
self.files[self.current_file] = new_file; self.files[self.current_file] = new_file;
} }
@ -108,7 +109,7 @@ impl AppState {
let new_file = EditorFile::new( let new_file = EditorFile::new(
file.path(), file.path(),
buffer, buffer,
renderer.config(), self.config.clone(),
); );
if let Some(rect) = get_text_character_rect(character, renderer) { if let Some(rect) = get_text_character_rect(character, renderer) {
if let Some(current) = file.get_character_at(position) { if let Some(current) = file.get_character_at(position) {
@ -120,6 +121,29 @@ impl AppState {
} }
self.files[self.current_file] = new_file; self.files[self.current_file] = new_file;
} }
fn current_file(&mut self) -> Option<&mut EditorFile> {
self.files.get_mut(self.current_file)
}
fn on_editor_clicked(&mut self, point: &Point) -> UpdateResult {
let current_file: &mut EditorFile = if let Some(current_file) = self.current_file() {
current_file
} else {
return UpdateResult::NoOp;
};
if !current_file.is_left_click_target(point) {
return UpdateResult::NoOp;
}
match current_file.on_left_click(point) {
UpdateResult::MoveCaret(rect, position) => {
self.caret.move_caret(position, Point::new(rect.x(), rect.y()));
}
_ => (),
};
UpdateResult::NoOp
}
} }
impl Render for AppState { impl Render for AppState {
@ -145,20 +169,11 @@ impl Update for AppState {
} }
impl ClickHandler for AppState { impl ClickHandler for AppState {
fn on_left_click(&mut self, point: &Point, config: &Config) -> UpdateResult { fn on_left_click(&mut self, point: &Point) -> UpdateResult {
if self.menu_bar.is_left_click_target(point) { if self.menu_bar.is_left_click_target(point) {
return self.menu_bar.on_left_click(point, config); return self.menu_bar.on_left_click(point);
}
if let Some(current_file) = self.files.get_mut(self.current_file) {
if current_file.is_left_click_target(point) {
match current_file.on_left_click(point, config) {
UpdateResult::MoveCaret(rect, position) => {
self.caret.move_caret(position, Point::new(rect.x(), rect.y()));
}
_ => (),
};
}
} }
self.on_editor_clicked(point);
UpdateResult::NoOp UpdateResult::NoOp
} }

View File

@ -4,7 +4,11 @@ use crate::renderer::Renderer;
use crate::themes::*; use crate::themes::*;
use crate::ui::*; use crate::ui::*;
use sdl2::{Sdl, TimerSubsystem}; use std::thread::sleep;
use std::time::Duration;
use std::rc::Rc;
use sdl2::{Sdl, VideoSubsystem, TimerSubsystem};
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::EventPump; use sdl2::EventPump;
use sdl2::keyboard::{Keycode, Mod}; use sdl2::keyboard::{Keycode, Mod};
@ -14,8 +18,7 @@ use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::Canvas; use sdl2::render::Canvas;
use sdl2::video::Window; use sdl2::video::Window;
use std::thread::sleep; use sdl2::ttf::Sdl2TtfContext;
use std::time::Duration;
pub mod app_state; pub mod app_state;
pub mod keyboard_handler; pub mod keyboard_handler;
@ -39,34 +42,39 @@ pub enum Task {
} }
pub struct Application { pub struct Application {
config: Config, config: Rc<Config>,
clear_color: Color,
sdl_context: Sdl, sdl_context: Sdl,
canvas: WindowCanvas, canvas: WindowCanvas,
video_subsystem: VideoSubsystem,
tasks: Vec<Task>, tasks: Vec<Task>,
clear_color: Color,
} }
impl Application { impl Application {
pub fn new() -> Self { pub fn new() -> Self {
let config = Config::new(); let config = Rc::new(Config::new());
let sdl_context = sdl2::init().unwrap(); let sdl_context = sdl2::init().unwrap();
hint::set("SDL_GL_MULTISAMPLEBUFFERS", "1"); hint::set("SDL_GL_MULTISAMPLEBUFFERS", "1");
hint::set("SDL_GL_MULTISAMPLESAMPLES", "8"); hint::set("SDL_GL_MULTISAMPLESAMPLES", "8");
hint::set("SDL_GL_ACCELERATED_VISUAL", "1"); hint::set("SDL_GL_ACCELERATED_VISUAL", "1");
hint::set("SDL_HINT_RENDER_SCALE_QUALITY", "2"); hint::set("SDL_HINT_RENDER_SCALE_QUALITY", "2");
hint::set("SDL_HINT_VIDEO_ALLOW_SCREENSAVER", "1"); hint::set("SDL_HINT_VIDEO_ALLOW_SCREENSAVER", "1");
let video_subsystem = sdl_context.video().unwrap(); let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem let window = video_subsystem
.window("Editor", config.width(), config.height()) .window("Rider", config.width(), config.height())
.position_centered() .position_centered()
.opengl() .opengl()
.build() .build()
.unwrap(); .unwrap();
let canvas = window.into_canvas().accelerated().build().unwrap(); let canvas = window.into_canvas().accelerated().build().unwrap();
// let font_context = Rc::new(sdl2::ttf::init().unwrap());
Self { Self {
sdl_context, sdl_context,
video_subsystem,
canvas, canvas,
tasks: vec![], tasks: vec![],
clear_color: config.theme().background().into(), clear_color: config.theme().background().into(),
@ -84,7 +92,7 @@ impl Application {
let font_context = sdl2::ttf::init().unwrap(); let font_context = sdl2::ttf::init().unwrap();
let texture_creator = self.canvas.texture_creator(); let texture_creator = self.canvas.texture_creator();
let sleep_time = Duration::new(0, 1_000_000_000u32 / 60); let sleep_time = Duration::new(0, 1_000_000_000u32 / 60);
let mut app_state = AppState::new(&self.config); let mut app_state = AppState::new(self.config.clone());
let mut renderer = Renderer::new(self.config.clone(), &font_context, &texture_creator); let mut renderer = Renderer::new(self.config.clone(), &font_context, &texture_creator);
'running: loop { 'running: loop {
@ -94,13 +102,13 @@ impl Application {
UpdateResult::NoOp => (), UpdateResult::NoOp => (),
UpdateResult::MoveCaret(_, _pos) => (), UpdateResult::MoveCaret(_, _pos) => (),
UpdateResult::MouseLeftClicked(point) => { UpdateResult::MouseLeftClicked(point) => {
app_state.on_left_click(&point, renderer.config()); app_state.on_left_click(&point);
} }
UpdateResult::DeleteFront => { UpdateResult::DeleteFront => {
app_state.delete_front(renderer.config()); app_state.delete_front();
}, },
UpdateResult::DeleteBack => { UpdateResult::DeleteBack => {
app_state.delete_back(renderer.config()); app_state.delete_back();
}, },
UpdateResult::Input(text_character) => { UpdateResult::Input(text_character) => {
app_state.insert_character(text_character, &mut renderer); app_state.insert_character(text_character, &mut renderer);
@ -110,7 +118,7 @@ impl Application {
match task { match task {
Task::OpenFile { file_path } => { Task::OpenFile { file_path } => {
use crate::ui::file::editor_file::*; use crate::ui::file::editor_file::*;
app_state.open_file(file_path.clone(), renderer.config()); app_state.open_file(file_path.clone());
} }
} }
} }

View File

@ -2,16 +2,16 @@ use crate::app::WindowCanvas;
use crate::config::Config; use crate::config::Config;
use crate::renderer::managers::{FontManager, TextureManager}; use crate::renderer::managers::{FontManager, TextureManager};
use crate::renderer::managers::TextDetails; use crate::renderer::managers::TextDetails;
use std::rc::Rc;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::{Texture, TextureCreator}; use sdl2::render::{Texture, TextureCreator};
use sdl2::ttf::Sdl2TtfContext; use sdl2::ttf::Sdl2TtfContext;
use sdl2::video::WindowContext; use sdl2::video::WindowContext;
use std::rc::Rc;
pub mod managers; pub mod managers;
pub struct Renderer<'a> { pub struct Renderer<'a> {
config: Config, config: Rc<Config>,
font_manager: FontManager<'a>, font_manager: FontManager<'a>,
texture_manager: TextureManager<'a, WindowContext>, texture_manager: TextureManager<'a, WindowContext>,
scroll: Point, scroll: Point,
@ -19,7 +19,7 @@ pub struct Renderer<'a> {
impl<'a> Renderer<'a> { impl<'a> Renderer<'a> {
pub fn new( pub fn new(
config: Config, config: Rc<Config>,
font_context: &'a Sdl2TtfContext, font_context: &'a Sdl2TtfContext,
texture_creator: &'a TextureCreator<WindowContext>, texture_creator: &'a TextureCreator<WindowContext>,
) -> Self { ) -> Self {
@ -31,7 +31,7 @@ impl<'a> Renderer<'a> {
} }
} }
pub fn config(&self) -> &Config { pub fn config(&self) -> &Rc<Config> {
&self.config &self.config
} }

View File

@ -3,6 +3,7 @@ use crate::config::Config;
use crate::renderer::Renderer; use crate::renderer::Renderer;
use crate::ui::*; use crate::ui::*;
use crate::ui::text_character::TextCharacter; use crate::ui::text_character::TextCharacter;
use std::rc::Rc;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::Texture; use sdl2::render::Texture;
@ -17,6 +18,7 @@ pub struct Caret {
pending: bool, pending: bool,
text_position: usize, text_position: usize,
blink_delay: u8, blink_delay: u8,
config: Rc<Config>,
state: CaretState, state: CaretState,
dest: Rect, dest: Rect,
reset_position: Rect, reset_position: Rect,
@ -25,7 +27,7 @@ pub struct Caret {
} }
impl Caret { impl Caret {
pub fn new(config: &Config) -> Self { pub fn new(config: Rc<Config>) -> Self {
let bright_character_color = config.theme().caret().bright().color().into(); let bright_character_color = config.theme().caret().bright().color().into();
let blur_character_color = config.theme().caret().blur().color().into(); let blur_character_color = config.theme().caret().blur().color().into();
Self { Self {
@ -47,6 +49,7 @@ impl Caret {
blur_character_color, blur_character_color,
pending: true, pending: true,
text_position: 0, text_position: 0,
config,
} }
} }
@ -121,7 +124,7 @@ impl Update for Caret {
} }
impl ClickHandler for Caret { impl ClickHandler for Caret {
fn on_left_click(&mut self, _point: &Point, _config: &Config) -> UpdateResult { fn on_left_click(&mut self, _point: &Point) -> UpdateResult {
// self.move_caret(Point::new(self.position.x(), self.position.y())); // self.move_caret(Point::new(self.position.x(), self.position.y()));
UpdateResult::NoOp UpdateResult::NoOp
} }

View File

@ -1,3 +1,4 @@
use std::rc::Rc;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use crate::app::{UpdateResult, WindowCanvas}; use crate::app::{UpdateResult, WindowCanvas};
@ -13,12 +14,13 @@ pub struct EditorFile {
sections: Vec<EditorFileSection>, sections: Vec<EditorFileSection>,
render_position: Rect, render_position: Rect,
buffer: String, buffer: String,
config: Rc<Config>
} }
impl EditorFile { impl EditorFile {
pub fn new(path: String, buffer: String, config: &Config) -> Self { pub fn new(path: String, buffer: String, config: Rc<Config>) -> Self {
let sections = vec![ let sections = vec![
EditorFileSection::new(buffer.clone(), config) EditorFileSection::new(buffer.clone(), config.clone())
]; ];
let x = config.editor_left_margin(); let x = config.editor_left_margin();
let y = config.editor_top_margin(); let y = config.editor_top_margin();
@ -26,7 +28,8 @@ impl EditorFile {
path, path,
sections, sections,
render_position: Rect::new(x, y, 0, 0), render_position: Rect::new(x, y, 0, 0),
buffer buffer,
config
} }
} }
@ -47,10 +50,10 @@ impl EditorFile {
None None
} }
fn refresh_characters_position(&mut self, config: &Config) { fn refresh_characters_position(&mut self) {
let mut current: Rect = self.render_position.clone(); let mut current: Rect = self.render_position.clone();
for section in self.sections.iter_mut() { for section in self.sections.iter_mut() {
section.update_positions(&mut current, config); section.update_positions(&mut current);
} }
} }
} }
@ -62,7 +65,7 @@ impl Render for EditorFile {
res = section.render(canvas, renderer); res = section.render(canvas, renderer);
} }
if res == UpdateResult::RefreshPositions { if res == UpdateResult::RefreshPositions {
self.refresh_characters_position(renderer.config()); self.refresh_characters_position();
for section in self.sections.iter_mut() { for section in self.sections.iter_mut() {
section.render(canvas, renderer); section.render(canvas, renderer);
} }
@ -82,10 +85,10 @@ impl Update for EditorFile {
} }
impl ClickHandler for EditorFile { impl ClickHandler for EditorFile {
fn on_left_click(&mut self, point: &Point, config: &Config) -> UpdateResult { fn on_left_click(&mut self, point: &Point) -> UpdateResult {
for section in self.sections.iter_mut() { for section in self.sections.iter_mut() {
if section.is_left_click_target(point) { if section.is_left_click_target(point) {
return section.on_left_click(point, config); return section.on_left_click(point);
} }
} }
UpdateResult::NoOp UpdateResult::NoOp

View File

@ -1,3 +1,4 @@
use std::rc::Rc;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use crate::app::{UpdateResult, WindowCanvas}; use crate::app::{UpdateResult, WindowCanvas};
@ -12,25 +13,26 @@ use crate::ui::text_character::TextCharacter;
pub struct EditorFileSection { pub struct EditorFileSection {
tokens: Vec<EditorFileToken>, tokens: Vec<EditorFileToken>,
language: Language, language: Language,
config: Rc<Config>,
} }
impl EditorFileSection { impl EditorFileSection {
pub fn new(buffer: String, config: &Config) -> Self { pub fn new(buffer: String, config: Rc<Config>) -> Self {
use crate::lexer; use crate::lexer;
let lexer_tokens = lexer::parse(buffer.clone(), Language::PlainText); let lexer_tokens = lexer::parse(buffer.clone(), Language::PlainText);
let mut tokens: Vec<EditorFileToken> = vec![]; let mut tokens: Vec<EditorFileToken> = vec![];
for token_type in lexer_tokens { for token_type in lexer_tokens {
let token = EditorFileToken::new(token_type, config); let token = EditorFileToken::new(token_type, config.clone());
tokens.push(token.clone()); tokens.push(token.clone());
} }
let language = Language::PlainText; let language = Language::PlainText;
Self { tokens, language } Self { tokens, language, config }
} }
pub fn update_positions(&mut self, current: &mut Rect, config: &Config) { pub fn update_positions(&mut self, current: &mut Rect) {
for c in self.tokens.iter_mut() { for c in self.tokens.iter_mut() {
c.update_position(current, config); c.update_position(current);
} }
} }
@ -68,10 +70,10 @@ impl Update for EditorFileSection {
} }
impl ClickHandler for EditorFileSection { impl ClickHandler for EditorFileSection {
fn on_left_click(&mut self, point: &Point, config: &Config) -> UpdateResult { fn on_left_click(&mut self, point: &Point) -> UpdateResult {
for token in self.tokens.iter_mut() { for token in self.tokens.iter_mut() {
if token.is_left_click_target(point) { if token.is_left_click_target(point) {
return token.on_left_click(point, config); return token.on_left_click(point);
} }
} }
UpdateResult::NoOp UpdateResult::NoOp

View File

@ -5,29 +5,31 @@ use crate::renderer::managers::{FontDetails, TextDetails};
use crate::renderer::Renderer; use crate::renderer::Renderer;
use crate::ui::*; use crate::ui::*;
use crate::ui::text_character::*; use crate::ui::text_character::*;
use std::rc::Rc;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use sdl2::render::Texture; use sdl2::render::Texture;
use sdl2::ttf::Font; use sdl2::ttf::Font;
use std::rc::Rc;
#[derive(Clone)] #[derive(Clone)]
pub struct EditorFileToken { pub struct EditorFileToken {
characters: Vec<TextCharacter>, characters: Vec<TextCharacter>,
token_type: TokenType, token_type: TokenType,
config: Rc<Config>
} }
impl EditorFileToken { impl EditorFileToken {
pub fn new(token_type: TokenType, _config: &Config) -> Self { pub fn new(token_type: TokenType, config: Rc<Config>) -> Self {
Self { Self {
characters: vec![], characters: vec![],
token_type, token_type,
config
} }
} }
pub fn update_position(&mut self, current: &mut Rect, config: &Config) { pub fn update_position(&mut self, current: &mut Rect) {
for text_character in self.characters.iter_mut() { for text_character in self.characters.iter_mut() {
text_character.update_position(current, config); text_character.update_position(current);
} }
} }
@ -58,7 +60,8 @@ impl EditorFileToken {
c.clone(), c.clone(),
self.token_type.start() + index, self.token_type.start() + index,
self.token_type.line(), self.token_type.line(),
color.clone() color.clone(),
self.config.clone()
); );
text_character.update_view(renderer); text_character.update_view(renderer);
self.characters.push(text_character); self.characters.push(text_character);
@ -97,10 +100,10 @@ impl Update for EditorFileToken {
} }
impl ClickHandler for EditorFileToken { impl ClickHandler for EditorFileToken {
fn on_left_click(&mut self, point: &Point, config: &Config) -> UpdateResult { fn on_left_click(&mut self, point: &Point) -> UpdateResult {
for text_character in self.characters.iter_mut() { for text_character in self.characters.iter_mut() {
if text_character.is_left_click_target(point) { if text_character.is_left_click_target(point) {
return text_character.on_left_click(point, config); return text_character.on_left_click(point);
} }
} }
UpdateResult::NoOp UpdateResult::NoOp

View File

@ -2,19 +2,22 @@ use crate::app::{UpdateResult, WindowCanvas};
use crate::config::Config; use crate::config::Config;
use crate::renderer::Renderer; use crate::renderer::Renderer;
use crate::ui::*; use crate::ui::*;
use std::rc::Rc;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::Rect; use sdl2::rect::{Rect, Point};
pub struct MenuBar { pub struct MenuBar {
background_color: Color, background_color: Color,
dest: Rect, dest: Rect,
config: Rc<Config>,
} }
impl MenuBar { impl MenuBar {
pub fn new() -> Self { pub fn new(config: Rc<Config>) -> Self {
Self { Self {
background_color: Color::RGB(10, 10, 10), background_color: Color::RGB(10, 10, 10),
dest: Rect::new(0, 0, 0, 0), dest: Rect::new(0, 0, 0, 0),
config,
} }
} }
@ -28,9 +31,9 @@ impl MenuBar {
} }
impl Render for MenuBar { impl Render for MenuBar {
fn render(&mut self, canvas: &mut WindowCanvas, renderer: &mut Renderer) -> UpdateResult { fn render(&mut self, canvas: &mut WindowCanvas, _renderer: &mut Renderer) -> UpdateResult {
let width = renderer.config().width(); let width = self.config.width();
let height = renderer.config().menu_height() as u32; let height = self.config.menu_height() as u32;
self.dest = Rect::new(0, 0, width, height); self.dest = Rect::new(0, 0, width, height);
canvas.set_draw_color(self.background_color.clone()); canvas.set_draw_color(self.background_color.clone());
canvas.draw_rect(self.dest.clone()).unwrap(); canvas.draw_rect(self.dest.clone()).unwrap();
@ -45,7 +48,7 @@ impl Update for MenuBar {
} }
impl ClickHandler for MenuBar { impl ClickHandler for MenuBar {
fn on_left_click(&mut self, _point: &Point, _config: &Config) -> UpdateResult { fn on_left_click(&mut self, _point: &Point) -> UpdateResult {
unimplemented!() unimplemented!()
} }

View File

@ -44,7 +44,7 @@ pub trait Update {
} }
pub trait ClickHandler { pub trait ClickHandler {
fn on_left_click(&mut self, point: &Point, config: &Config) -> UpdateResult; fn on_left_click(&mut self, point: &Point) -> UpdateResult;
fn is_left_click_target(&self, point: &Point) -> bool; fn is_left_click_target(&self, point: &Point) -> bool;
} }

View File

@ -6,11 +6,11 @@ use crate::renderer::managers::TextDetails;
use crate::renderer::Renderer; use crate::renderer::Renderer;
use crate::ui::*; use crate::ui::*;
use std::rc::Rc;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::{Rect, Point}; use sdl2::rect::{Rect, Point};
use sdl2::render::Texture; use sdl2::render::Texture;
use sdl2::ttf::Font; use sdl2::ttf::Font;
use std::rc::Rc;
#[derive(Clone)] #[derive(Clone)]
pub struct TextCharacter { pub struct TextCharacter {
@ -21,10 +21,11 @@ pub struct TextCharacter {
source: Rect, source: Rect,
dest: Rect, dest: Rect,
color: Color, color: Color,
config: Rc<Config>
} }
impl TextCharacter { impl TextCharacter {
pub fn new(text_character: char, position: usize, line: usize, color: Color) -> Self { pub fn new(text_character: char, position: usize, line: usize, color: Color, config: Rc<Config>) -> Self {
Self { Self {
pending: true, pending: true,
text_character, text_character,
@ -33,6 +34,7 @@ impl TextCharacter {
source: Rect::new(0, 0, 0, 0), source: Rect::new(0, 0, 0, 0),
dest: Rect::new(0, 0, 0, 0), dest: Rect::new(0, 0, 0, 0),
color, color,
config
} }
} }
@ -48,10 +50,10 @@ impl TextCharacter {
&self.color &self.color
} }
pub fn update_position(&mut self, current: &mut Rect, config: &Config) { pub fn update_position(&mut self, current: &mut Rect) {
if self.is_new_line() { if self.is_new_line() {
let y = self.source.height() as i32; let y = self.source.height() as i32;
current.set_x(config.editor_left_margin()); current.set_x(self.config.editor_left_margin());
current.set_y(current.y() + y); current.set_y(current.y() + y);
} else { } else {
self.dest.set_x(current.x()); self.dest.set_x(current.x());
@ -145,7 +147,7 @@ impl Update for TextCharacter {
} }
impl ClickHandler for TextCharacter { impl ClickHandler for TextCharacter {
fn on_left_click(&mut self, _point: &Point, _config: &Config) -> UpdateResult { fn on_left_click(&mut self, _point: &Point) -> UpdateResult {
UpdateResult::MoveCaret( UpdateResult::MoveCaret(
self.dest().clone(), self.dest().clone(),
self.position() self.position()