z_mirror/TaskList
git clone https://git.raiguard.me/z_mirror/TaskList
c176be5
task-details-gui
1local gui =require ( "__flib__.gui" ) 2local table =require ( "__flib__.table" ) 3 4local constants =require ( "constants" ) 5 6local actions =require ( "actions" ) 7 8-- GUI 9 10--- @class EditTaskGuiRefs 11--- @field window LuaGuiElement 12--- @field titlebar_flow LuaGuiElement 13--- @field title_textfield LuaGuiElement 14--- @field description_textfield LuaGuiElement 15--- @field add_to_top_checkbox LuaGuiElement 16--- @field private_checkbox LuaGuiElement 17--- @field assignee_dropdown LuaGuiElement 18--- @field status_dropdown LuaGuiElement 19--- @field footer_flow LuaGuiElement 20 21--- @class EditTaskGui 22local EditTaskGui ={} 23 24EditTaskGui . actions =actions 25 26function EditTaskGui : destroy () 27local window =self . refs . window 28if window and window . valid then 29self . refs . window . destroy () 30end 31self . player_table . guis . edit_task =nil 32 33if self . parent and not self . parent . state . pinned then 34self . player . opened =self . parent . refs . window --- @diagnostic disable-line 35end 36 37if self . state . standalone then 38self . player . set_shortcut_toggled ( "tlst-new-task" , false ) 39end 40end 41 42function EditTaskGui : dispatch ( msg , e ) 43local transform =msg . transform 44if transform then 45if transform == "handle_titlebar_click" and e . button == defines . mouse_button_type . middle then 46msg . action ="recenter" 47end 48end 49 50if msg . action then 51local handler =self . actions [ msg . action ] 52if handler then 53handler ( self , msg or {} , e or {} ) 54end 55end 56end 57 58-- BOOTSTRAP 59 60local index ={} 61 62--- @class NewTaskOptions 63--- @field parent_gui LuaGuiElement|nil 64--- @field task Task|nil 65--- @field parent_task Task|nil 66--- @field set_private boolean 67--- @field standalone boolean 68 69--- @param player LuaPlayer 70--- @param player_table PlayerTable 71--- @param options NewTaskOptions 72function index . new ( player , player_table , options ) 73options =options or {} 74local Task =options . task 75local Parent =options . parent_gui 76local ParentTask =options . parent_task 77 78local players ={ { "gui.tlst-unassigned" } } 79local force =player . force 80local assignee_selection_index =1 81local player_selection_index =0 82 83local title_caption 84if Task then 85title_caption ={ "gui.tlst-edit-task" } 86else 87title_caption ={ "gui.tlst-new-task" } 88end 89 90Task =Task or {} 91 92local assignable =true 93local owner =Task . owner or ParentTask or {} 94local assignee_index =0 95while owner . object_name == "Task" do 96owner =owner . owner 97end 98if owner . object_name == "LuaPlayer" then 99assignable =false 100assignee_index =ParentTask and ParentTask . assignee . index or 0 101else 102assignee_index =Task . assignee and Task . assignee . index or 0 103end 104 105if assignable and not assignee_index and owner . assignee then 106assignee_index =owner . assignee . index 107end 108 109for player_index , other_player in pairs ( game . players ) do 110if other_player . force == force then 111table . insert ( players , other_player . name ) 112if player_index == player . index then 113player_selection_index =# players 114end 115if assignee_index == player_index then 116assignee_selection_index =# players 117end 118end 119end 120 121local status_items ={} 122local selected_status_index =1 123for status_name , status_info in pairs ( constants . task_status ) do 124if status_name == Task . status then 125selected_status_index =status_info . index 126end 127table . insert ( status_items , { "" , "[img=flib_indicator_" .. status_info . color .. "] " , status_info . label } ) 128end 129 130--- @type EditTaskGuiRefs 131local refs =gui . build ( player . gui . screen , { 132{ 133type ="frame" , 134style_mods ={ width =448 } , 135direction ="vertical" , 136ref ={ "window" } , 137actions ={ 138on_closed ={ gui ="edit_task" , action ="close" } , 139} , 140{ 141type ="flow" , 142style ="flib_titlebar_flow" , 143ref ={ "titlebar_flow" } , 144actions ={ 145on_click ={ gui ="edit_task" , transform ="handle_titlebar_click" } , 146} , 147{ type ="label" , style ="frame_title" , caption =title_caption , ignored_by_interaction =true } , 148{ type ="empty-widget" , style ="flib_dialog_titlebar_drag_handle" , ignored_by_interaction =true } , 149} , 150{ 151type ="frame" , 152style ="inside_shallow_frame" , 153direction ="vertical" , 154{ 155type ="frame" , 156style ="subheader_frame" , 157style_mods ={ horizontally_stretchable =true } , 158visible =ParentTask and true or false , 159{ 160type ="label" , 161style ="bold_label" , 162style_mods ={ left_margin =8 } , 163caption =ParentTask and { "gui.tlst-subtask-of" , ParentTask . title } or nil , 164} , 165} , 166{ 167type ="flow" , 168style_mods ={ padding =12 , vertical_spacing =8 } , 169direction ="vertical" , 170{ type ="label" , caption ={ "gui.tlst-title" } } , 171{ 172type ="textfield" , 173style ="flib_widthless_textfield" , 174style_mods ={ horizontally_stretchable =true } , 175text =Task . title , 176ref ={ "title_textfield" } , 177actions ={ 178on_confirmed ={ gui ="edit_task" , action ="confirm" } , 179} , 180} , 181{ type ="label" , caption ={ "gui.tlst-description" } } , 182{ 183type ="text-box" , 184style_mods ={ height =200 , width =400 } , 185text =Task . description , 186elem_mods ={ word_wrap =true } , 187ref ={ "description_textfield" } , 188actions ={ 189on_confirmed ={ gui ="edit_task" , action ="confirm" } , 190} , 191} , 192{ 193type ="flow" , 194visible =not Task . title , 195{ 196type ="checkbox" , 197caption ={ "gui.tlst-add-to-top" } , 198state =false , 199ref ={ "add_to_top_checkbox" } , 200} , 201{ type ="empty-widget" , style ="flib_horizontal_pusher" } , 202{ 203type ="checkbox" , 204caption ={ "gui.tlst-private" } , 205tooltip ={ "gui.tlst-private-description" } , 206state =owner and owner . object_name == "LuaPlayer" or false , 207enabled =not ParentTask , 208ref ={ "private_checkbox" } , 209actions ={ 210on_checked_state_changed ={ gui ="edit_task" , action ="update_assignee_dropdown" } , 211} , 212} , 213} , 214{ 215type ="flow" , 216style_mods ={ vertical_align ="center" } , 217{ type ="label" , caption ={ "gui.tlst-assignee" } } , 218{ type ="empty-widget" , style ="flib_horizontal_pusher" } , 219{ 220type ="drop-down" , 221items =players , 222selected_index =assignee_selection_index , 223enabled =assignable , 224ref ={ "assignee_dropdown" } , 225} , 226} , 227{ 228type ="flow" , 229style_mods ={ vertical_align ="center" } , 230{ 231type ="label" , 232caption ={ "" , { "gui.tlst-status" } , " [img=info]" } , 233tooltip ={ "gui.tlst-status-description" } , 234} , 235{ type ="empty-widget" , style ="flib_horizontal_pusher" } , 236{ 237type ="drop-down" , 238items =status_items , 239selected_index =selected_status_index , 240ref ={ "status_dropdown" } , 241} , 242} , 243} , 244} , 245{ 246type ="flow" , 247style ="dialog_buttons_horizontal_flow" , 248actions ={ on_click ={ gui ="edit_task" , transform ="handle_titlebar_click" } } , 249ref ={ "footer_flow" } , 250{ 251type ="button" , 252style ="back_button" , 253caption ={ "gui.cancel" } , 254actions ={ 255on_click ={ gui ="edit_task" , action ="close" } , 256} , 257} , 258{ type ="empty-widget" , style ="flib_dialog_footer_drag_handle" , ignored_by_interaction =true } , 259Task . title and { 260type ="button" , 261style ="tlst_red_dialog_button" , 262caption ={ "gui.delete" } , 263actions ={ 264on_click ={ gui ="edit_task" , action ="delete" } , 265} , 266} or {} , 267Task . title and { 268type ="empty-widget" , 269style ="flib_dialog_footer_drag_handle" , 270ignored_by_interaction =true , 271} or {} , 272{ 273type ="button" , 274style ="confirm_button" , 275caption ={ "gui.confirm" } , 276actions ={ 277on_click ={ gui ="edit_task" , action ="confirm" } , 278} , 279} , 280} , 281} , 282} ) 283 284refs . window . force_auto_center () 285refs . titlebar_flow . drag_target =refs . window 286refs . footer_flow . drag_target =refs . window 287 288if Parent and not Parent . state . pinned then 289Parent . state . ignore_close =true 290player . opened =refs . window 291elseif options . standalone then 292player . opened =refs . window 293player . set_shortcut_toggled ( "tlst-new-task" , true ) 294end 295 296--- @type EditTaskGui 297local self ={ 298parent =Parent , 299player =player , 300player_table =player_table , 301refs =refs , 302state ={ 303just_confirmed =false , 304parent_task =ParentTask , 305player_selection_index =player_selection_index , 306standalone =options . standalone , 307--- @type Task|nil 308task =Task . title and Task or nil , 309} , 310} 311 312setmetatable ( self , { __index =EditTaskGui } ) 313 314player_table . guis . edit_task =self 315 316if options . set_private then 317self . refs . private_checkbox . state =true 318self . actions . update_assignee_dropdown ( self ) 319end 320 321self . refs . title_textfield . select_all () 322self . refs . title_textfield . focus () 323end 324 325--- @param Gui EditTaskGui 326function index . load ( Gui ) 327setmetatable ( Gui , { __index =EditTaskGui } ) 328end 329 330return index