Skip to content

Commit

Permalink
Fixing text buffer for dark mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
phase1geo committed Feb 13, 2024
1 parent 80b7f28 commit ec4186e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
63 changes: 48 additions & 15 deletions src/Editor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using Gtk;
using Gdk;

public class Editor : GtkSource.View {
public class Editor {

/* Structure to hold absolute position */
public class Position {
Expand Down Expand Up @@ -55,27 +55,55 @@ public class Editor : GtkSource.View {

}

private UndoBuffer _undo_buffer;
private bool _ignore_edit = false;
private string _lang_dict;
private SpellChecker _spell = null;
private GtkSource.View _view;
private GtkSource.Buffer _buffer;
private UndoBuffer _undo_buffer;
private bool _ignore_edit = false;
private string _lang_dict;
private SpellChecker _spell = null;
private bool _dark_mode = false;

public GtkSource.View view {
get {
return( _view );
}
}
public TextBuffer buffer {
get {
return( _view.buffer );
}
}
public UndoBuffer undo_buffer {
get {
return( _undo_buffer );
}
}
public bool dark_mode {
get {
return( _dark_mode );
}
set {
if( _dark_mode != value ) {
_dark_mode = value;
var style_mgr = GtkSource.StyleSchemeManager.get_default();
_buffer.style_scheme = style_mgr.get_scheme( _dark_mode ? "Adwaita-dark" : "Adwaita" );
}
}
}

public signal void buffer_changed( UndoBuffer buf );

/* Constructor */
public Editor( MainWindow win ) {

/* TBD - We may want to make this a preference */
wrap_mode = WrapMode.WORD;
top_margin = 20;
left_margin = 10;
right_margin = 10;
_buffer = new GtkSource.Buffer( null );

_view = new GtkSource.View.with_buffer( _buffer ) {
wrap_mode = WrapMode.WORD,
top_margin = 20,
left_margin = 10,
right_margin = 10
};

/* Add the undo_buffer */
_undo_buffer = new UndoBuffer( this );
Expand All @@ -84,7 +112,7 @@ public class Editor : GtkSource.View {
});

/* Set a CSS style class so that we can adjust the font */
get_style_context().add_class( "editor" );
_view.get_style_context().add_class( "editor" );

/* Set the default font */
var font_name = TextShine.settings.get_string( "default-font-family" );
Expand Down Expand Up @@ -112,13 +140,18 @@ public class Editor : GtkSource.View {
undo_item.add_edit( false, start.get_offset(), start.get_text( end ) );
});

extra_menu = new GLib.Menu();
_view.extra_menu = new GLib.Menu();

/* Connect spell checker */
connect_spell_checker();

}

/* Grabs keyboard focus */
public bool grab_focus() {
return( _view.grab_focus() );
}

/* Updates the font theme */
public void change_name_font( string name, int size ) {

Expand All @@ -132,7 +165,7 @@ public class Editor : GtkSource.View {
}

/* Set the CSS */
get_style_context().add_provider(
_view.get_style_context().add_provider(
provider,
STYLE_PROVIDER_PRIORITY_APPLICATION
);
Expand Down Expand Up @@ -444,15 +477,15 @@ public class Editor : GtkSource.View {
/* Attaches the spell checker to ourselves */
public void set_spellchecker() {
if( TextShine.settings.get_boolean( "enable-spell-checking" ) ) {
_spell.attach( this );
_spell.attach( _view );
} else {
_spell.detach();
}
}

/* Creates and populates the extra menu associated with this text widget */
private void populate_extra_menu() {
extra_menu = new GLib.Menu();
_view.extra_menu = new GLib.Menu();
}

/*
Expand Down
9 changes: 6 additions & 3 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,22 @@ public class MainWindow : Gtk.ApplicationWindow {
var box = new Box( Orientation.HORIZONTAL, 5 );

/* Handle any changes to the dark mode preference setting */
handle_prefer_dark_changes();
var dark_mode = handle_prefer_dark_changes();

/* Create the header */
create_header();

/* Create editor */
_editor = new Editor( this );
_editor.buffer_changed.connect( do_buffer_changed );
_editor.dark_mode = dark_mode;

var sw = new ScrolledWindow() {
valign = Align.FILL,
vexpand = true,
min_content_width = 600,
min_content_height = 400,
child = _editor
child = _editor.view
};

var ebox = new Box( Orientation.VERTICAL, 0 ) {
Expand Down Expand Up @@ -173,13 +174,15 @@ public class MainWindow : Gtk.ApplicationWindow {
}

/* Handles any changes to the dark mode preference gsettings for the desktop */
private void handle_prefer_dark_changes() {
private bool handle_prefer_dark_changes() {
var granite_settings = Granite.Settings.get_default();
var gtk_settings = Gtk.Settings.get_default();
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
_editor.dark_mode = gtk_settings.gtk_application_prefer_dark_theme;
});
return( gtk_settings.gtk_application_prefer_dark_theme );
}

/* Returns the name of the icon to use based on if we are running elementary */
Expand Down

0 comments on commit ec4186e

Please sign in to comment.