rat-times/ui/Main.gd

119 lines
3.3 KiB
GDScript3
Raw Normal View History

2023-02-24 20:39:49 +00:00
extends Control
@onready var time_label: Label = %TimeLabel
@onready var start_button: Button = %StartButton
@onready var task_name_line_edit: LineEdit = %TaskNameLineEdit
2023-03-03 21:12:54 +00:00
@onready var time_entries_items_tree: TimeEntriesItemsTree = %TimeEntriesItemsTree
2023-02-24 20:39:49 +00:00
@onready var timer: Timer = %Timer
@onready var tasks_button: Button = %TasksButton
@onready var settings_button: Button = %SettingsButton
@onready var previous_tasks_window: Window = %PreviousTasksWindow
@onready var settings_window: Window = %SettingsWindow
@onready var audio_stream_player: AudioStreamPlayer = %AudioStreamPlayer
2023-03-03 21:12:54 +00:00
var config: ConfigManager = preload("res://config_manager.tres")
2023-02-24 20:39:49 +00:00
func _ready() -> void:
2023-03-04 13:31:27 +00:00
config.file_changed.connect(set_initial_state)
config.theme_changed.connect(
func theme_changed() -> void:
theme = config.them
2023-02-24 20:39:49 +00:00
)
2023-03-04 13:31:27 +00:00
get_tree().set_auto_accept_quit(false)
2023-02-24 20:39:49 +00:00
previous_tasks_window.hide()
settings_window.hide()
timer.timeout.connect(
func on_timer_timeout() -> void:
2023-03-04 13:31:27 +00:00
config.timesheet.update()
2023-02-24 20:39:49 +00:00
2023-03-04 13:31:27 +00:00
time_label.text = config.timesheet.get_period()
2023-02-24 20:39:49 +00:00
2023-03-04 13:31:27 +00:00
var total_elapsed: int = config.timesheet.get_total_elapsed_seconds()
2023-03-03 21:12:54 +00:00
time_entries_items_tree.set_time_elapsed(total_elapsed)
2023-02-24 20:39:49 +00:00
)
2023-03-03 21:12:54 +00:00
start_button.tooltip_text = tr(Consts.START)
2023-02-24 20:39:49 +00:00
start_button.toggle_mode = true
start_button.toggled.connect(
func start(is_on: bool) -> void:
2023-03-04 13:31:27 +00:00
if config.sound_fx_on:
2023-02-24 20:39:49 +00:00
audio_stream_player.play()
if is_on:
2023-03-04 13:31:27 +00:00
config.timesheet.start_entry(task_name_line_edit.text)
2023-03-03 21:12:54 +00:00
set_button_as_started()
2023-02-24 20:39:49 +00:00
else:
2023-03-04 13:31:27 +00:00
config.timesheet.close_entry()
2023-03-03 21:12:54 +00:00
set_button_as_stopped()
2023-02-24 20:39:49 +00:00
)
2023-03-03 21:12:54 +00:00
task_name_line_edit.text = config.last_task_name
2023-02-24 20:39:49 +00:00
task_name_line_edit.text_changed.connect(
func(new_text: String) -> void:
2023-03-03 21:12:54 +00:00
config.last_task_name = new_text
2023-02-24 20:39:49 +00:00
)
2023-03-03 21:12:54 +00:00
time_entries_items_tree.item_selected.connect(
2023-02-24 20:39:49 +00:00
func item_selected() -> void:
2023-03-03 21:12:54 +00:00
task_name_line_edit.text = time_entries_items_tree.get_current_text()
2023-02-24 20:39:49 +00:00
)
tasks_button.toggle_mode = true
tasks_button.toggled.connect(
func tasks_toggled(is_on: bool) -> void:
previous_tasks_window.visible = is_on
)
previous_tasks_window.close_requested.connect(
func close() -> void:
tasks_button.set_pressed_no_signal(false)
previous_tasks_window.hide()
)
settings_button.toggle_mode = true
settings_button.toggled.connect(
func settings_toggled(is_on: bool) -> void:
settings_window.visible = is_on
)
settings_window.close_requested.connect(
func close() -> void:
settings_button.set_pressed_no_signal(false)
settings_window.hide()
)
2023-03-03 21:12:54 +00:00
func set_button_as_started() -> void:
2023-03-04 13:31:27 +00:00
time_entries_items_tree.set_current_item(config.timesheet.current_entry.name)
2023-03-03 21:12:54 +00:00
start_button.tooltip_text = tr(Consts.STOP)
start_button.theme_type_variation = Consts.THEME_OVERRIDE_STOP
timer.start()
2023-02-24 20:39:49 +00:00
2023-03-03 21:12:54 +00:00
func set_button_as_stopped() -> void:
start_button.tooltip_text = tr(Consts.START)
time_label.text = Consts.NO_TIME
start_button.theme_type_variation = Consts.THEME_OVERRIDE_START
timer.stop()
2023-02-24 20:39:49 +00:00
2023-03-04 13:31:27 +00:00
func set_initial_state() -> void:
if config.timesheet.current_entry != null and config.timesheet.current_entry.closed == false:
2023-03-03 21:12:54 +00:00
start_button.set_pressed_no_signal(true)
set_button_as_started()
2023-02-24 20:39:49 +00:00
func _notification(what: int) -> void:
if what == NOTIFICATION_WM_CLOSE_REQUEST:
get_tree().quit()
## Unused; if a manual quit button is added, this would be used
func quit() -> void:
get_tree().notification(NOTIFICATION_WM_CLOSE_REQUEST)