以下の例に従って、アドオン Text User Interface (TUI) のサポートを追加するために必要なすべての定義を含むモジュールを作成します。
例5.5 シンプルな TUI Spoke の定義
class HelloWorldSpoke(NormalTUISpoke):
# category this spoke belongs to
category = HelloWorldCategory
def __init__(self, *args, **kwargs):
"""
Create the representation of the spoke.
:see: simpleline.render.screen.UIScreen
"""
super().__init__(*args, **kwargs)
self.title = N_("Hello World")
self._hello_world_module = HELLO_WORLD.get_proxy()
self._container = None
self._reverse = False
self._lines = ""
def initialize(self):
"""
The initialize method that is called after the instance is created.
The difference between __init__ and this method is that this may take
a long time and thus could be called in a separated thread.
:see: pyanaconda.ui.common.UIObject.initialize
"""
# nothing to do here
super().initialize()
def setup(self, args=None):
"""
The setup method that is called right before the spoke is entered.
It should update its state according to the contents of DBus modules.
:see: simpleline.render.screen.UIScreen.setup
"""
super().setup(args)
self._reverse = self._hello_world_module.Reverse
self._lines = self._hello_world_module.Lines
return True
def refresh(self, args=None):
"""
The refresh method that is called every time the spoke is displayed.
It should generate the UI elements according to its state.
:see: pyanaconda.ui.common.UIObject.refresh
:see: simpleline.render.screen.UIScreen.refresh
"""
super().refresh(args)
self._container = ListColumnContainer(
columns=1
)
self._container.add(
CheckboxWidget(
title="Reverse",
completed=self._reverse
),
callback=self._change_reverse
)
self._container.add(
EntryWidget(
title="Hello world text",
value="".join(self._lines)
),
callback=self._change_lines
)
self.window.add_with_separator(self._container)
def _change_reverse(self, data):
"""
Callback when user wants to switch checkbox.
Flip state of the "reverse" parameter which is boolean.
"""
self._reverse = not self._reverse
def _change_lines(self, data):
"""
Callback when user wants to input new lines.
Show a dialog and save the provided lines.
"""
dialog = Dialog("Lines")
result = dialog.run()
self._lines = result.splitlines(True)
def input(self, args, key):
"""
The input method that is called by the main loop on user's input.
* If the input should not be handled here, return it.
* If the input is invalid, return InputState.DISCARDED.
* If the input is handled and the current screen should be refreshed,
return InputState.PROCESSED_AND_REDRAW.
* If the input is handled and the current screen should be closed,
return InputState.PROCESSED_AND_CLOSE.
:see: simpleline.render.screen.UIScreen.input
"""
if self._container.process_user_input(key):
return InputState.PROCESSED_AND_REDRAW
if key.lower() == Prompt.CONTINUE:
self.apply()
self.execute()
return InputState.PROCESSED_AND_CLOSE
return super().input(args, key)
def apply(self):
"""
The apply method is not called automatically for TUI. It should be called
in input() if required. It should update the contents of internal data
structures with values set in the spoke.
"""
self._hello_world_module.SetReverse(self._reverse)
self._hello_world_module.SetLines(self._lines)
def execute(self):
"""
The execute method is not called automatically for TUI. It should be called
in input() if required. It is supposed to do all changes to the runtime
environment according to the values set in the spoke.
"""
# nothing to do here
pass
class HelloWorldSpoke(NormalTUISpoke):
# category this spoke belongs to
category = HelloWorldCategory
def __init__(self, *args, **kwargs):
"""
Create the representation of the spoke.
:see: simpleline.render.screen.UIScreen
"""
super().__init__(*args, **kwargs)
self.title = N_("Hello World")
self._hello_world_module = HELLO_WORLD.get_proxy()
self._container = None
self._reverse = False
self._lines = ""
def initialize(self):
"""
The initialize method that is called after the instance is created.
The difference between __init__ and this method is that this may take
a long time and thus could be called in a separated thread.
:see: pyanaconda.ui.common.UIObject.initialize
"""
# nothing to do here
super().initialize()
def setup(self, args=None):
"""
The setup method that is called right before the spoke is entered.
It should update its state according to the contents of DBus modules.
:see: simpleline.render.screen.UIScreen.setup
"""
super().setup(args)
self._reverse = self._hello_world_module.Reverse
self._lines = self._hello_world_module.Lines
return True
def refresh(self, args=None):
"""
The refresh method that is called every time the spoke is displayed.
It should generate the UI elements according to its state.
:see: pyanaconda.ui.common.UIObject.refresh
:see: simpleline.render.screen.UIScreen.refresh
"""
super().refresh(args)
self._container = ListColumnContainer(
columns=1
)
self._container.add(
CheckboxWidget(
title="Reverse",
completed=self._reverse
),
callback=self._change_reverse
)
self._container.add(
EntryWidget(
title="Hello world text",
value="".join(self._lines)
),
callback=self._change_lines
)
self.window.add_with_separator(self._container)
def _change_reverse(self, data):
"""
Callback when user wants to switch checkbox.
Flip state of the "reverse" parameter which is boolean.
"""
self._reverse = not self._reverse
def _change_lines(self, data):
"""
Callback when user wants to input new lines.
Show a dialog and save the provided lines.
"""
dialog = Dialog("Lines")
result = dialog.run()
self._lines = result.splitlines(True)
def input(self, args, key):
"""
The input method that is called by the main loop on user's input.
* If the input should not be handled here, return it.
* If the input is invalid, return InputState.DISCARDED.
* If the input is handled and the current screen should be refreshed,
return InputState.PROCESSED_AND_REDRAW.
* If the input is handled and the current screen should be closed,
return InputState.PROCESSED_AND_CLOSE.
:see: simpleline.render.screen.UIScreen.input
"""
if self._container.process_user_input(key):
return InputState.PROCESSED_AND_REDRAW
if key.lower() == Prompt.CONTINUE:
self.apply()
self.execute()
return InputState.PROCESSED_AND_CLOSE
return super().input(args, key)
def apply(self):
"""
The apply method is not called automatically for TUI. It should be called
in input() if required. It should update the contents of internal data
structures with values set in the spoke.
"""
self._hello_world_module.SetReverse(self._reverse)
self._hello_world_module.SetLines(self._lines)
def execute(self):
"""
The execute method is not called automatically for TUI. It should be called
in input() if required. It is supposed to do all changes to the runtime
environment according to the values set in the spoke.
"""
# nothing to do here
pass
Copy to ClipboardCopied!Toggle word wrapToggle overflow