Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7ae42f4

Browse files
committedMar 18, 2024
Add keybindings
1 parent 8358f57 commit 7ae42f4

File tree

5 files changed

+95
-58
lines changed

5 files changed

+95
-58
lines changed
 

‎tools/config_editor/app.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232

3333
try:
3434
from textual.app import App, ComposeResult
35+
from textual.binding import Binding
3536
from textual.containers import VerticalScroll
3637
from textual.screen import Screen
37-
from textual.widgets import Button, Header, Label
38+
from textual.widgets import Button, Header, Label, Footer
3839
except ImportError:
39-
print("Please install the \"textual-dev\" package before running this script.")
40+
print("Please install the \"textual\" package before running this script.")
4041
exit(1)
4142

4243
from settings import SettingsScreen
@@ -46,6 +47,14 @@
4647
class MainScreen(Screen):
4748
# Main screen class
4849

50+
# Set the key bindings
51+
BINDINGS = [
52+
Binding("c", "app.push_screen('compile')", "Compile"),
53+
Binding("e", "app.push_screen('editor')", "Editor"),
54+
Binding("s", "app.push_screen('settings')", "Settings"),
55+
Binding("q", "app.quit", "Quit"),
56+
]
57+
4958
def on_button_pressed(self, event: Button.Pressed) -> None:
5059
# Event handler called when a button is pressed
5160
if event.button.id == "compile-button":
@@ -67,9 +76,10 @@ def compose(self) -> ComposeResult:
6776
with VerticalScroll(id="main-menu-container"):
6877
yield Label("ESP32 Arduino Static Libraries Configuration Editor", id="main-menu-title")
6978
yield Button("Compile Static Libraries", id="compile-button", classes="main-menu-button")
70-
yield Button("Change sdkconfig Flags", id="editor-button", classes="main-menu-button")
79+
yield Button("Sdkconfig Editor", id="editor-button", classes="main-menu-button")
7180
yield Button("Settings", id="settings-button", classes="main-menu-button")
7281
yield Button("Quit", id="quit-button", classes="main-menu-button")
82+
yield Footer()
7383

7484
def on_mount(self) -> None:
7585
# Event handler called when the app is mounted for the first time

‎tools/config_editor/compile.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,40 @@
66

77
from textual import on, work
88
from textual.app import ComposeResult
9+
from textual.binding import Binding
910
from textual.events import ScreenResume
1011
from textual.containers import Container
1112
from textual.screen import Screen
12-
from textual.widgets import Header, Static, RichLog, Button
13+
from textual.widgets import Header, Static, RichLog, Button, Footer
1314

1415
class CompileScreen(Screen):
1516
# Compile screen
1617

18+
# Set the key bindings
19+
BINDINGS = [
20+
Binding("escape", "back", "Back")
21+
]
22+
1723
# Child process running the libraries compilation
1824
child_process = None
1925

2026
log_widget: RichLog
2127
button_widget: Button
2228

29+
def action_back(self) -> None:
30+
self.workers.cancel_all()
31+
if self.child_process:
32+
# Terminate the child process if it is running
33+
print("Terminating child process")
34+
self.child_process.terminate()
35+
try:
36+
self.child_process.stdout.close()
37+
self.child_process.stderr.close()
38+
except:
39+
pass
40+
self.child_process.wait()
41+
self.dismiss()
42+
2343
def print_output(self, renderable: RenderableType, style=None) -> None:
2444
# Print output to the RichLog widget
2545
if style is None:
@@ -115,18 +135,7 @@ def compile_libs(self) -> None:
115135

116136
def on_button_pressed(self, event: Button.Pressed) -> None:
117137
# Event handler called when a button is pressed
118-
self.workers.cancel_all()
119-
if self.child_process:
120-
# Terminate the child process if it is running
121-
print("Terminating child process")
122-
self.child_process.terminate()
123-
try:
124-
self.child_process.stdout.close()
125-
self.child_process.stderr.close()
126-
except:
127-
pass
128-
self.child_process.wait()
129-
self.dismiss()
138+
self.action_back()
130139

131140
@on(ScreenResume)
132141
def on_resume(self) -> None:
@@ -148,3 +157,4 @@ def compose(self) -> ComposeResult:
148157
yield Static("Compiling for ...", id="compile-title")
149158
self.button_widget = Button("Back", id="compile-back-button")
150159
yield self.button_widget
160+
yield Footer()

‎tools/config_editor/editor.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,44 @@
22

33
from textual import on
44
from textual.app import ComposeResult
5+
from textual.binding import Binding
56
from textual.containers import Container, VerticalScroll, Horizontal
67
from textual.screen import Screen
78
from textual.events import ScreenResume
8-
from textual.widgets import DirectoryTree, Header, TextArea, Button
9+
from textual.widgets import DirectoryTree, Header, TextArea, Button, Footer
910

1011
class EditorScreen(Screen):
1112
# Configuration file editor screen
1213

14+
# Set the key bindings
15+
BINDINGS = [
16+
Binding("ctrl+s", "save", "Save", priority=True),
17+
Binding("escape", "app.pop_screen", "Discard")
18+
]
19+
1320
# Current file being edited
1421
current_file = ""
1522

23+
def action_save(self) -> None:
24+
code_view = self.query_one("#code", TextArea)
25+
current_text = code_view.text
26+
try:
27+
file = open(self.curent_file, "w")
28+
file.write(current_text)
29+
file.close()
30+
except Exception:
31+
print("Error saving file: " + self.curent_file)
32+
self.sub_title = "ERROR"
33+
else:
34+
print("File saved: " + self.curent_file)
35+
self.sub_title = self.curent_file
36+
self.dismiss()
37+
1638
def on_button_pressed(self, event: Button.Pressed) -> None:
1739
# Event handler called when a button is pressed
18-
code_view = self.query_one("#code", TextArea)
1940
if event.button.id == "save-editor-button" and self.curent_file != "":
20-
current_text = code_view.text
21-
try:
22-
print("Save button pressed. Trying to save file: " + self.curent_file)
23-
file = open(self.curent_file, "w")
24-
file.write(current_text)
25-
file.close()
26-
except Exception:
27-
print("Error saving file: " + self.curent_file)
28-
self.sub_title = "ERROR"
29-
else:
30-
print("File saved: " + self.curent_file)
31-
self.sub_title = self.curent_file
32-
self.dismiss()
41+
print("Save button pressed. Trying to save file: " + self.curent_file)
42+
self.action_save()
3343
elif event.button.id == "cancel-editor-button":
3444
print("Cancel button pressed")
3545
self.dismiss()
@@ -73,3 +83,4 @@ def compose(self) -> ComposeResult:
7383
with Horizontal(id="editor-buttons-container"):
7484
yield Button("Save", id="save-editor-button", classes="editor-button")
7585
yield Button("Cancel", id="cancel-editor-button", classes="editor-button")
86+
yield Footer()

‎tools/config_editor/settings.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from textual import on
22
from textual.app import ComposeResult
3+
from textual.binding import Binding
34
from textual.containers import VerticalScroll, Container, Horizontal
45
from textual.screen import Screen
56
from textual.events import ScreenResume
6-
from textual.widgets import Header, Button, Switch, Label
7+
from textual.widgets import Header, Button, Switch, Label, Footer
78

89
from widgets import LabelledInput, LabelledSelect
910

1011
class SettingsScreen(Screen):
1112
# Settings screen
1213

14+
# Set the key bindings
15+
BINDINGS = [
16+
Binding("s", "save", "Save"),
17+
Binding("escape", "app.pop_screen", "Discard")
18+
]
19+
1320
target_select: LabelledSelect
1421
enable_copy_switch: Switch
1522
arduino_path_input: LabelledInput
@@ -18,37 +25,37 @@ class SettingsScreen(Screen):
1825
idf_commit_input: LabelledInput
1926
idf_debug_select: LabelledSelect
2027

21-
def on_button_pressed(self, event: Button.Pressed) -> None:
22-
# Event handler called when a button is pressed
23-
if event.button.id == "save-settings-button":
24-
print("Save button pressed")
25-
26-
self.app.setting_target = self.target_select.get_select_value()
27-
print("Target setting updated: " + self.app.setting_target)
28+
def action_save(self) -> None:
29+
self.app.setting_target = self.target_select.get_select_value()
30+
print("Target setting updated: " + self.app.setting_target)
2831

29-
self.app.setting_enable_copy = self.enable_copy_switch.value
30-
print("Enable copy setting updated: " + str(self.app.setting_enable_copy))
32+
self.app.setting_enable_copy = self.enable_copy_switch.value
33+
print("Enable copy setting updated: " + str(self.app.setting_enable_copy))
3134

32-
if self.enable_copy_switch.value:
33-
self.app.setting_arduino_path = self.arduino_path_input.get_input_value()
34-
print("Arduino path setting updated: " + self.app.setting_arduino_path)
35+
if self.enable_copy_switch.value:
36+
self.app.setting_arduino_path = self.arduino_path_input.get_input_value()
37+
print("Arduino path setting updated: " + self.app.setting_arduino_path)
3538

36-
self.app.setting_arduino_branch = self.arduino_branch_input.get_input_value()
37-
print("Arduino branch setting updated: " + self.app.setting_arduino_branch)
39+
self.app.setting_arduino_branch = self.arduino_branch_input.get_input_value()
40+
print("Arduino branch setting updated: " + self.app.setting_arduino_branch)
3841

39-
self.app.setting_idf_branch = self.idf_branch_input.get_input_value()
40-
print("IDF branch setting updated: " + self.app.setting_idf_branch)
42+
self.app.setting_idf_branch = self.idf_branch_input.get_input_value()
43+
print("IDF branch setting updated: " + self.app.setting_idf_branch)
4144

42-
self.app.setting_idf_commit = self.idf_commit_input.get_input_value()
43-
print("IDF commit setting updated: " + self.app.setting_idf_commit)
45+
self.app.setting_idf_commit = self.idf_commit_input.get_input_value()
46+
print("IDF commit setting updated: " + self.app.setting_idf_commit)
4447

45-
self.app.setting_debug_level = self.idf_debug_select.get_select_value()
46-
print("Debug level setting updated: " + self.app.setting_debug_level)
48+
self.app.setting_debug_level = self.idf_debug_select.get_select_value()
49+
print("Debug level setting updated: " + self.app.setting_debug_level)
4750

48-
self.dismiss()
51+
def on_button_pressed(self, event: Button.Pressed) -> None:
52+
# Event handler called when a button is pressed
53+
if event.button.id == "save-settings-button":
54+
print("Save button pressed")
55+
self.action_save()
4956
elif event.button.id == "cancel-settings-button":
5057
print("Cancel button pressed")
51-
self.dismiss()
58+
self.dismiss()
5259

5360
@on(ScreenResume)
5461
def on_resume(self) -> None:
@@ -124,6 +131,7 @@ def compose(self) -> ComposeResult:
124131
with Horizontal(id="settings-button-container"):
125132
yield Button("Save", id="save-settings-button", classes="settings-button")
126133
yield Button("Cancel", id="cancel-settings-button", classes="settings-button")
134+
yield Footer()
127135

128136
def on_mount(self) -> None:
129137
# Event handler called when the screen is mounted for the first time

‎tools/config_editor/style.tcss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,12 @@ Button {
6969
border-bottom: tall $error-lighten-2;
7070
border-top: tall $error-darken-2;
7171
}
72-
7372
}
74-
7573
}
7674

7775
# Main Screen
7876

79-
Button.main-menu-button {
77+
.main-menu-button {
8078
margin-bottom: 1;
8179
min-width: 100%;
8280
max-width: 0.4fr;
@@ -91,7 +89,7 @@ Button.main-menu-button {
9189
text-align: center;
9290
margin-bottom: 4;
9391
text-style: bold;
94-
color: lightgray;
92+
color: auto;
9593
width: 0.4fr;
9694
}
9795

0 commit comments

Comments
 (0)
Please sign in to comment.