From cef15f46a5e55bc8da9d664df6b962192f4ac516 Mon Sep 17 00:00:00 2001 From: Rasmus Thomsen Date: Sat, 29 Jul 2023 13:13:06 +0200 Subject: [PATCH 1/3] library: add SourceView example Fixes #383 --- src/Library/demos/Source View/main.blp | 27 +++++++++++++++++++++++++ src/Library/demos/Source View/main.js | 23 +++++++++++++++++++++ src/Library/demos/Source View/main.json | 7 +++++++ src/Library/demos/Source View/main.vala | 24 ++++++++++++++++++++++ src/about.js | 1 + 5 files changed, 82 insertions(+) create mode 100644 src/Library/demos/Source View/main.blp create mode 100644 src/Library/demos/Source View/main.js create mode 100644 src/Library/demos/Source View/main.json create mode 100644 src/Library/demos/Source View/main.vala diff --git a/src/Library/demos/Source View/main.blp b/src/Library/demos/Source View/main.blp new file mode 100644 index 000000000..f5fe582c6 --- /dev/null +++ b/src/Library/demos/Source View/main.blp @@ -0,0 +1,27 @@ +using Gtk 4.0; +using Adw 1; +using GtkSource 5; + +Adw.StatusPage { + title: "Source View"; + description: _("A widget that enables text-editing with advanced features like syntax highlighting"); + + Box { + orientation: vertical; + halign: center; + + Frame { + margin-bottom: 12; + + ScrolledWindow sw { + height-request: 180; + width-request: 600; + } + } + + LinkButton{ + label: "API Reference"; + uri: "https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/class.View.html"; + } + } +} diff --git a/src/Library/demos/Source View/main.js b/src/Library/demos/Source View/main.js new file mode 100644 index 000000000..86b1ea438 --- /dev/null +++ b/src/Library/demos/Source View/main.js @@ -0,0 +1,23 @@ +import Gtk from "gi://Gtk"; +import Adw from "gi://Adw"; +import GtkSource from "gi://GtkSource"; + +GtkSource.init(); + +// Get the language we want to use +const language_manager = GtkSource.LanguageManager.get_default(); +const language = language_manager.get_language("js"); + // Create the buffer - this holds the text that's used in the SourceView +const buffer = GtkSource.Buffer.new_with_language(language); +buffer.set_text('console.log("Hello World!");', -1); + +// Create the SourceView which displays the buffer's display +const view = GtkSource.View.new_with_buffer(buffer); +view.set_auto_indent(true); +view.set_indent_width = 4; +view.show_line_numbers = true; + +// Add the SourceView to our ScrolledView so its displayed +const sw = workbench.builder.get_object("sw"); +sw.set_child(view); + diff --git a/src/Library/demos/Source View/main.json b/src/Library/demos/Source View/main.json new file mode 100644 index 000000000..bdab53892 --- /dev/null +++ b/src/Library/demos/Source View/main.json @@ -0,0 +1,7 @@ +{ + "name": "Source View", + "category": "controls", + "description": "A widget with advanced text-editing features like syntax highlighting", + "panels": ["code", "ui", "preview"], + "autorun": false +} diff --git a/src/Library/demos/Source View/main.vala b/src/Library/demos/Source View/main.vala new file mode 100644 index 000000000..d53d540e3 --- /dev/null +++ b/src/Library/demos/Source View/main.vala @@ -0,0 +1,24 @@ +#!/usr/bin/env -S vala workbench.vala --pkg gtk4 --pkg libadwaita-1 --pkg gtksourceview-5 + +public void main () { + stdout.printf("test"); + Gtk.init (); + GtkSource.init (); + + // Get the language we want to use + var language_manager = GtkSource.LanguageManager.get_default(); + var language = language_manager.get_language("js"); + // Create the buffer - this holds the text that's used in the SourceView + var buffer = new GtkSource.Buffer.with_language(language); + buffer.set_text("console.log(\"Hello World!\");"); + + // Create the SourceView which displays the buffer's display + var view = new GtkSource.View.with_buffer(buffer); + view.auto_indent = true; + view.indent_width = 4; + view.show_line_numbers = true; + + // Add the SourceView to our ScrolledView so its displayed + var sw = workbench.builder.get_object ("sw") as Gtk.ScrolledWindow; + sw.child = view; +} diff --git a/src/about.js b/src/about.js index 485c515cd..5f25df1c4 100644 --- a/src/about.js +++ b/src/about.js @@ -70,6 +70,7 @@ ${getBlueprintVersion()} "JCWasmx86 https://github.com/JCWasmx86", "Alex (PaladinDev) https://github.com/SpikedPaladin", "Diego Iván M.E https://github.com/Diego-Ivan", + "Rasmus Thomsen ", // Add yourself as // "John Doe", // or From c1a18646dde93247138d04322cf391f61eeb7147 Mon Sep 17 00:00:00 2001 From: Rasmus Thomsen Date: Sat, 29 Jul 2023 13:36:30 +0200 Subject: [PATCH 2/3] library: add comment to GtkSource.init() --- src/Library/demos/Source View/main.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Library/demos/Source View/main.js b/src/Library/demos/Source View/main.js index 86b1ea438..725d95995 100644 --- a/src/Library/demos/Source View/main.js +++ b/src/Library/demos/Source View/main.js @@ -2,6 +2,8 @@ import Gtk from "gi://Gtk"; import Adw from "gi://Adw"; import GtkSource from "gi://GtkSource"; +// Strictly speaking we don't _have_ to do this here since WorkBench does this for us. +// However, you _have_ to call this once during the startup in your application - e.g. in GApplication::startup GtkSource.init(); // Get the language we want to use From 0ab2be123862a5210bb6d8a850ffb3ce0b6ab6c8 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Sun, 13 Aug 2023 16:51:38 +0200 Subject: [PATCH 3/3] improvements --- src/Library/demos/Source View/main.blp | 15 ++++----- src/Library/demos/Source View/main.js | 20 ++++++------ src/Library/demos/Source View/main.json | 6 ++-- src/Library/demos/Source View/main.vala | 41 ++++++++++++------------- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/Library/demos/Source View/main.blp b/src/Library/demos/Source View/main.blp index f5fe582c6..5d9bf1f14 100644 --- a/src/Library/demos/Source View/main.blp +++ b/src/Library/demos/Source View/main.blp @@ -4,22 +4,19 @@ using GtkSource 5; Adw.StatusPage { title: "Source View"; - description: _("A widget that enables text-editing with advanced features like syntax highlighting"); + description: _("Widget that enables text-editing with advanced features like syntax highlighting"); Box { orientation: vertical; halign: center; - Frame { - margin-bottom: 12; - - ScrolledWindow sw { - height-request: 180; - width-request: 600; - } + ScrolledWindow source_view { + height-request: 180; + width-request: 600; + has-frame: true; } - LinkButton{ + LinkButton { label: "API Reference"; uri: "https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5/class.View.html"; } diff --git a/src/Library/demos/Source View/main.js b/src/Library/demos/Source View/main.js index 725d95995..46a309abe 100644 --- a/src/Library/demos/Source View/main.js +++ b/src/Library/demos/Source View/main.js @@ -1,5 +1,3 @@ -import Gtk from "gi://Gtk"; -import Adw from "gi://Adw"; import GtkSource from "gi://GtkSource"; // Strictly speaking we don't _have_ to do this here since WorkBench does this for us. @@ -9,17 +7,19 @@ GtkSource.init(); // Get the language we want to use const language_manager = GtkSource.LanguageManager.get_default(); const language = language_manager.get_language("js"); - // Create the buffer - this holds the text that's used in the SourceView + +// Create the buffer - this holds the text that's used in the SourceView const buffer = GtkSource.Buffer.new_with_language(language); buffer.set_text('console.log("Hello World!");', -1); // Create the SourceView which displays the buffer's display -const view = GtkSource.View.new_with_buffer(buffer); -view.set_auto_indent(true); -view.set_indent_width = 4; -view.show_line_numbers = true; +const source_view = GtkSource.View.new({ + auto_indent: true, + indent_width: 4, + buffer, + show_line_numbers: true, +}); // Add the SourceView to our ScrolledView so its displayed -const sw = workbench.builder.get_object("sw"); -sw.set_child(view); - +const scrolled_window = workbench.builder.get_object("scrolled_window"); +scrolled_window.set_child(source_view); diff --git a/src/Library/demos/Source View/main.json b/src/Library/demos/Source View/main.json index bdab53892..5a88c92e3 100644 --- a/src/Library/demos/Source View/main.json +++ b/src/Library/demos/Source View/main.json @@ -1,7 +1,7 @@ { "name": "Source View", "category": "controls", - "description": "A widget with advanced text-editing features like syntax highlighting", - "panels": ["code", "ui", "preview"], - "autorun": false + "description": "Widget that enables text-editing with advanced features like syntax highlighting", + "panels": ["code", "preview"], + "autorun": true } diff --git a/src/Library/demos/Source View/main.vala b/src/Library/demos/Source View/main.vala index d53d540e3..6e96f474b 100644 --- a/src/Library/demos/Source View/main.vala +++ b/src/Library/demos/Source View/main.vala @@ -1,24 +1,23 @@ #!/usr/bin/env -S vala workbench.vala --pkg gtk4 --pkg libadwaita-1 --pkg gtksourceview-5 - + public void main () { - stdout.printf("test"); - Gtk.init (); - GtkSource.init (); - - // Get the language we want to use - var language_manager = GtkSource.LanguageManager.get_default(); - var language = language_manager.get_language("js"); - // Create the buffer - this holds the text that's used in the SourceView - var buffer = new GtkSource.Buffer.with_language(language); - buffer.set_text("console.log(\"Hello World!\");"); - - // Create the SourceView which displays the buffer's display - var view = new GtkSource.View.with_buffer(buffer); - view.auto_indent = true; - view.indent_width = 4; - view.show_line_numbers = true; - - // Add the SourceView to our ScrolledView so its displayed - var sw = workbench.builder.get_object ("sw") as Gtk.ScrolledWindow; - sw.child = view; + Gtk.init (); + GtkSource.init (); + + // Get the language we want to use + var language_manager = GtkSource.LanguageManager.get_default(); + var language = language_manager.get_language("js"); + // Create the buffer - this holds the text that's used in the SourceView + var buffer = new GtkSource.Buffer.with_language(language); + buffer.set_text("console.log(\"Hello World!\");"); + + // Create the SourceView which displays the buffer's display + var source_view = new GtkSource.View.with_buffer(buffer); + source_view.auto_indent = true; + source_view.indent_width = 4; + source_view.show_line_numbers = true; + + // Add the SourceView to our ScrolledView so its displayed + var scrolled_window = workbench.builder.get_object ("scrolled_window") as Gtk.ScrolledWindow; + scrolled_window.child = source_view; }