Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from shanmukhateja/gtk-notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmukhateja authored Mar 6, 2022
2 parents d25f3ed + 13eb597 commit f653e6f
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target

*.glade~
45 changes: 45 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'my-studio'",
"cargo": {
"args": [
"build",
"--bin=my-studio",
"--package=my-studio"
],
"filter": {
"name": "my-studio",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-studio'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=my-studio",
"--package=my-studio"
],
"filter": {
"name": "my-studio",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
25 changes: 20 additions & 5 deletions res/ui/main_window.glade
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,27 @@ Author: Surya Teja K <shanmukhateja@gmail.com>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkSourceView" id="main_text_editor">
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="vexpand">True</property>
<property name="left-margin">2</property>
<property name="right-margin">2</property>
<property name="can-focus">False</property>
<child>
<object class="GtkNotebook" id="editor_notebook">
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
<object class="GtkSourceView" id="main_text_editor">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="vexpand">True</property>
<property name="left-margin">2</property>
<property name="right-margin">2</property>
</object>
</child>
<child type="tab">
<placeholder/>
</child>
</object>
</child>
</object>
</child>
</object>
Expand Down
94 changes: 38 additions & 56 deletions src/comms.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::path::Path;

use gtk::glib::{self, Receiver, Sender};
use gtk::prelude::{ObjectExt, StatusbarExt, TextBufferExt, TextViewExt, WidgetExt};
use sourceview4::LanguageManager;
use gtk::prelude::{ObjectExt, StatusbarExt};
use gtk::traits::TextViewExt;

use crate::{
action_handler,
ui::{self, tree_model::RootTreeModel},
workspace::Workspace,
};
use sourceview4::prelude::*;

use crate::{G_STATUS_BAR, G_TEXT_VIEW, G_TREE};

// A 'global' way to trigger GUI events
pub enum CommEvents {
// Triggers TreeView#set_model
UpdateRootTree(),

// Spawn/Focus Notebook Tab,
SpawnOrFocusTab(Option<String>, Option<String>),
// used to read text files
RootTreeItemClicked(Option<RootTreeModel>),
// Sets text to RootTextView
Expand All @@ -34,9 +34,14 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
ui::tree_view::update_tree_model(&tree.borrow().clone().unwrap());
// Reset UI
tx.send(CommEvents::RootTreeItemClicked(None)).ok();
tx.send(CommEvents::UpdateRootTextViewContent(None, None)).ok();
tx.send(CommEvents::SpawnOrFocusTab(None, None)).ok();
tx.send(CommEvents::UpdateRootTextViewContent(None, None))
.ok();
});
}
CommEvents::SpawnOrFocusTab(file_path, content) => {
ui::notebook::handle_notebook_event(content, file_path);
}
CommEvents::RootTreeItemClicked(tree_model) => {
match tree_model {
Some(tree_model) => {
Expand All @@ -48,7 +53,8 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
if file_path.is_file() {
match std::fs::read(file_path) {
Ok(data) => {
content = String::from_utf8(data).unwrap_or_else(|_| "File not supported".to_string());
content = String::from_utf8(data)
.unwrap_or_else(|_| "File not supported".to_string());
// Update workspace's 'current open file' tracker
let open_file_path = file_path.as_os_str().to_str().unwrap();
Workspace::set_open_file_path(Some(String::from(
Expand All @@ -61,9 +67,13 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
}
}

let file_path_string: String = String::from(file_path.to_str().unwrap());
tx.send(CommEvents::UpdateRootTextViewContent(Some(file_path_string), Some(content)))
.ok();
let file_path_string = String::from(file_path.to_str().unwrap());

tx.send(CommEvents::SpawnOrFocusTab(
Some(file_path_string),
Some(content),
))
.ok();
}
None => {
// Reset workspace's 'current open file' tracker
Expand All @@ -74,59 +84,31 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
CommEvents::UpdateRootTextViewContent(path, content) => {
G_TEXT_VIEW.with(|editor| {
let text_editor = &editor.borrow().clone().unwrap();

match content {
Some(content) => {
let source_buffer = sourceview4::Buffer::builder()
.text(content.as_str())
.build();

// Detect language for syntax highlight
let lang_manager = LanguageManager::new();
match lang_manager.guess_language(Some(path.unwrap()), None) {
Some(lang) => {
source_buffer.set_language(Some(&lang));
},
None => {
source_buffer.set_language(sourceview4::Language::NONE);
}
}
// update buffer in View
text_editor.set_buffer(Some(&source_buffer));
// Show cursor on text_view so user can start modifying file
text_editor.grab_focus();
}
None => {
// Reset text content
text_editor.buffer().unwrap().set_text("");
}
}
ui::utils::set_text_on_editor(text_editor, path, content);
});
}
CommEvents::SaveEditorChanges() => {
G_TEXT_VIEW.with(|editor| {
let text_editor = &editor.borrow().clone().unwrap();

let text_buffer = text_editor.buffer().unwrap();
let file_absolute_path = Workspace::get_open_file_path();
match file_absolute_path {
Some(file_abs_path) => {
// Get View widget of open file
let text_editor =
ui::notebook::get_current_page_editor(file_abs_path.clone());
let text_buffer = text_editor.expect("Unable to find editor for open file").buffer().unwrap();

let file_absolute_path = Workspace::get_open_file_path();
match file_absolute_path {
Some(file_abs_path) => {
action_handler::save_file_changes(text_buffer, file_abs_path.clone());
G_STATUS_BAR.with(|status_bar| {
let status_bar_ref = status_bar.borrow();
let status_bar =
status_bar_ref.as_ref().expect("Unable to use status_bar");
action_handler::save_file_changes(text_buffer, file_abs_path.clone());
G_STATUS_BAR.with(|status_bar| {
let status_bar_ref = status_bar.borrow();
let status_bar =
status_bar_ref.as_ref().expect("Unable to use status_bar");

status_bar
.push(0, &format!("Saved changes to '{}'", &file_abs_path));
});
}
None => {
println!("Unable to write Workspace#open_file_path");
}
status_bar.push(0, &format!("Saved changes to '{}'", &file_abs_path));
});
}
});
None => {
println!("Unable to write Workspace#open_file_path");
}
}
}
}
// Don't forget to include this!
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use gtk::{
glib,
prelude::{
ApplicationCommandLineExt, ApplicationExt, ApplicationExtManual, BuilderExtManual,
GtkWindowExt, WidgetExt,
GtkWindowExt, WidgetExt, NotebookExtManual,
},
Application, ApplicationWindow, Builder, Statusbar, TreeView,
Application, ApplicationWindow, Builder, Statusbar, TreeView, Notebook
};

mod action_handler;
Expand All @@ -23,6 +23,7 @@ thread_local! { pub static G_WINDOW: RefCell<Option<ApplicationWindow>> = RefCel
thread_local! { pub static G_TREE: RefCell<Option<TreeView>> = RefCell::new(None) }
thread_local! { pub static G_TEXT_VIEW: RefCell<Option<sourceview4::View>> = RefCell::new(None) }
thread_local! { pub static G_STATUS_BAR: RefCell<Option<Statusbar>> = RefCell::new(None) }
thread_local! { pub static G_NOTEBOOK: RefCell<Option<Notebook>> = RefCell::new(None) }

fn build_ui(app: &Application) {
G_WINDOW.with(|window| {
Expand All @@ -49,6 +50,16 @@ fn build_ui(app: &Application) {
ui::tree_view::setup_tree(&builder, tx.clone());
});

G_NOTEBOOK.with(|notebook| {
*notebook.borrow_mut() = builder.object("editor_notebook");
let notebook = notebook.borrow().clone();
assert!(notebook.is_some());

let notebook = notebook.unwrap();
// Remove placeholder
notebook.remove_page(Some(0));
});

// Text Editor
G_TEXT_VIEW.with(|editor| {
*editor.borrow_mut() = builder.object("main_text_editor");
Expand Down
2 changes: 2 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod btn_action_row;
pub mod tree_model;
pub mod tree_view;
pub mod notebook;
pub mod utils;
Loading

0 comments on commit f653e6f

Please sign in to comment.