-
-
Notifications
You must be signed in to change notification settings - Fork 61
Widgets: Add TerminalView #867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9e3e8d6
764640e
1306caa
7102eef
1a92a46
727cb15
2c27d3e
1f93fde
9e24e49
37602bf
2af81a7
35626e7
5650bb3
2828f71
63a10f5
54f57e8
2e04a2d
04bcee4
70f7af5
899fdc2
a4609c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 20205 elementary, Inc. (https://elementary.io) | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| */ | ||
|
|
||
| public class TerminalOutputView: DemoPage { | ||
| construct { | ||
| title = "Terminal Output"; | ||
| var terminal = new Granite.TerminalView () { | ||
| autoscroll = true, | ||
| vexpand = true, | ||
| margin_top = 12, | ||
| margin_bottom = 12, | ||
| margin_start = 12, | ||
| margin_end = 12 | ||
| }; | ||
| terminal.text = "[ 25%] Performing optimization passes\n"; | ||
| terminal.text += "[ 65%] Inserting nonsense functions to pad binary size\n"; | ||
| terminal.text += "[ 73%] Linking C executable granite-demo\n"; | ||
| terminal.text += "[100%] Built target granite-demo\n"; | ||
| terminal.text += "Counting to one hundred…\n"; | ||
|
|
||
| for (int i = 0; i <= 100; i++) { | ||
| var itos = i.to_string ("%i\n"); | ||
| terminal.text += itos; | ||
| } | ||
|
|
||
| terminal.add_css_class (Granite.CssClass.CARD); | ||
|
|
||
| child = terminal; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| .terminal { | ||
| font-family: monospace; | ||
|
|
||
| background-color: $SLATE_900; | ||
| color: $SILVER_200; | ||
|
|
||
| // this is roughly 3 lines | ||
| min-height: 9ex; | ||
|
|
||
| & selection { | ||
| background-color: $SILVER_200; | ||
| color: $SLATE_900; | ||
|
|
||
| &:backdrop { | ||
| // Cancelling values set in non-terminal selection | ||
| background-color: inherit; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,4 +47,3 @@ paper { | |
| .monospace { | ||
| font-family: monospace; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Copyright 2025 elementary, Inc. (https://elementary.io) | ||
| * SPDX-License-Identifier: GPL-2.0-or-later | ||
| */ | ||
|
|
||
| [Version (since = "7.7.0")] | ||
| public class Granite.TerminalView : Granite.Bin { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs documentation. See some of the other widgets for an example. Just needs a simple description of what this widget is and what it's used for |
||
|
|
||
| public bool autoscroll { get; set; default = false; } | ||
| public string text { get; set; } | ||
|
|
||
| private Gtk.TextBuffer buffer; | ||
| private double prev_upper_adj = 0; | ||
| private Gtk.ScrolledWindow scrolled_window; | ||
|
|
||
| construct { | ||
| var view = new Gtk.TextView () { | ||
| cursor_visible = false, | ||
| editable = false, | ||
| monospace = true, | ||
| pixels_below_lines = 3, | ||
| left_margin = 9, | ||
| right_margin = 9, | ||
| top_margin = 6, | ||
| bottom_margin = 6, | ||
| wrap_mode = Gtk.WrapMode.WORD | ||
| }; | ||
|
|
||
| buffer = view.get_buffer (); | ||
|
|
||
| scrolled_window = new Gtk.ScrolledWindow () { | ||
| child = view, | ||
| hexpand = true, | ||
| vexpand = true, | ||
| hscrollbar_policy = NEVER, | ||
| }; | ||
|
|
||
| child = scrolled_window; | ||
| add_css_class (Granite.CssClass.TERMINAL); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding a style class, this should be the css name. Right now you have css nodes like |
||
|
|
||
| bind_property ("text", buffer, "text", BIDIRECTIONAL | SYNC_CREATE); | ||
|
|
||
| notify["autoscroll"].connect ((s, p) => { | ||
| update_autoscroll (); | ||
| }); | ||
| } | ||
|
|
||
| private void update_autoscroll () { | ||
| // FIXME: this disjoints the window closing and the application finishing | ||
| Idle.add (() => { | ||
| attempt_scroll (); | ||
| if (autoscroll) { | ||
| return GLib.Source.CONTINUE; | ||
| } else { | ||
| return GLib.Source.REMOVE; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void attempt_scroll () { | ||
| var adj = scrolled_window.vadjustment; | ||
| var units_from_end = prev_upper_adj - adj.page_size - adj.value; | ||
|
|
||
| if (adj.upper - prev_upper_adj <= 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (prev_upper_adj <= adj.page_size || units_from_end <= 50) { | ||
| adj.value = adj.upper; | ||
| } | ||
|
|
||
| prev_upper_adj = adj.upper; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be added as a constant here. We usually don't make constants for things that aren't used outside of their own class