@@ -170,14 +170,28 @@ def get_current_layout_from_config():
170170
171171 logger .debug ("Fallback to legacy hash comparison method" )
172172 logger .debug (f"Checking config: { CONFIG_JSONC } " )
173+
174+ layouts = find_layout_files ()
175+ if not layouts :
176+ logger .error ("No layout files found" )
177+ return None
178+
179+ # If config.jsonc doesn't exist, just use the first available layout
173180 if not CONFIG_JSONC .exists ():
174- logger .error ("Config file not found" )
181+ logger .debug ("Config file not found, using first available layout " )
175182 CONFIG_JSONC .parent .mkdir (parents = True , exist_ok = True )
176- with open (CONFIG_JSONC , "w" ) as f :
177- json .dump ({}, f )
183+
184+ layout = layouts [0 ]
185+ layout_name = os .path .basename (layout ).replace (".jsonc" , "" )
186+ set_state_value ("WAYBAR_LAYOUT_PATH" , layout )
187+ set_state_value ("WAYBAR_LAYOUT_NAME" , layout_name )
188+
189+ shutil .copyfile (layout , CONFIG_JSONC )
190+ logger .debug (f"Created config.jsonc with first layout: { layout } " )
191+ return layout
178192
193+ # Try hash comparison for existing config
179194 config_hash = get_file_hash (CONFIG_JSONC )
180- layouts = find_layout_files ()
181195 layout = None
182196
183197 for layout_file in layouts :
@@ -189,8 +203,9 @@ def get_current_layout_from_config():
189203 layout = layout_file
190204 return layout
191205
192- if not layout and layouts :
193- logger .debug ("No current layout found by hash comparison" )
206+ # If no hash match found, use first layout as fallback
207+ if not layout :
208+ logger .debug ("No current layout found by hash comparison, using first layout" )
194209 current_layout_name = "unknown"
195210 backup_layout (current_layout_name )
196211 layout = layouts [0 ]
@@ -1443,47 +1458,74 @@ def main():
14431458 logger .debug (f"State file found: { STATE_FILE } " )
14441459 layout_path = get_state_value ("WAYBAR_LAYOUT_PATH" )
14451460
1446- if layout_path and os .path .exists (layout_path ) and CONFIG_JSONC .exists ():
1447- config_hash = get_file_hash (CONFIG_JSONC )
1448- layout_hash = get_file_hash (layout_path )
1461+ if layout_path and os .path .exists (layout_path ):
1462+ # If config.jsonc doesn't exist, create it from the layout
1463+ if not CONFIG_JSONC .exists ():
1464+ logger .debug ("Config file missing, creating from layout path" )
1465+ CONFIG_JSONC .parent .mkdir (parents = True , exist_ok = True )
1466+ shutil .copyfile (layout_path , CONFIG_JSONC )
1467+ logger .debug ("Created config.jsonc from state file layout" )
1468+ else :
1469+ config_hash = get_file_hash (CONFIG_JSONC )
1470+ layout_hash = get_file_hash (layout_path )
14491471
1450- if config_hash != layout_hash :
1451- logger .debug ("Config hash differs from layout hash, creating backup" )
1452- layout_name = os .path .basename (layout_path ).replace (".jsonc" , "" )
1453- backup_layout (layout_name )
1472+ if config_hash != layout_hash :
1473+ logger .debug ("Config hash differs from layout hash, creating backup" )
1474+ layout_name = os .path .basename (layout_path ).replace (".jsonc" , "" )
1475+ backup_layout (layout_name )
14541476
1455- try :
1456- shutil .copyfile (layout_path , CONFIG_JSONC )
1457- logger .debug ("Updated config.jsonc with layout from state file" )
1458- except Exception as e :
1459- logger .error (f"Failed to update config.jsonc: { e } " )
1477+ try :
1478+ shutil .copyfile (layout_path , CONFIG_JSONC )
1479+ logger .debug ("Updated config.jsonc with layout from state file" )
1480+ except Exception as e :
1481+ logger .error (f"Failed to update config.jsonc: { e } " )
14601482
1461- elif layout_path and not os .path .exists (layout_path ) and CONFIG_JSONC . exists () :
1483+ elif layout_path and not os .path .exists (layout_path ):
14621484 logger .warning (f"Layout path in state file doesn't exist: { layout_path } " )
14631485 layout_name = get_state_value ("WAYBAR_LAYOUT_NAME" )
14641486 if layout_name :
14651487 logger .debug (f"Looking for layout by name: { layout_name } " )
14661488 layouts = find_layout_files ()
1489+ found_layout = None
14671490 for layout in layouts :
14681491 if os .path .basename (layout ).replace (".jsonc" , "" ) == layout_name :
14691492 logger .debug (f"Found layout by name: { layout } " )
1470-
1493+ found_layout = layout
1494+ break
1495+
1496+ if found_layout :
1497+ # Update state and create/update config
1498+ set_state_value ("WAYBAR_LAYOUT_PATH" , found_layout )
1499+ CONFIG_JSONC .parent .mkdir (parents = True , exist_ok = True )
1500+
1501+ if CONFIG_JSONC .exists ():
14711502 config_hash = get_file_hash (CONFIG_JSONC )
1472- layout_hash = get_file_hash (layout )
1473-
1503+ layout_hash = get_file_hash (found_layout )
14741504 if config_hash != layout_hash :
14751505 backup_layout (layout_name )
1476-
1477- set_state_value ("WAYBAR_LAYOUT_PATH" , layout )
1478-
1479- try :
1480- shutil .copyfile (layout , CONFIG_JSONC )
1481- logger .debug ("Updated config.jsonc with layout by name" )
1482- except Exception as e :
1483- logger .error (f"Failed to update config.jsonc: { e } " )
1484- break
1506+
1507+ shutil .copyfile (found_layout , CONFIG_JSONC )
1508+ logger .debug ("Updated config.jsonc with layout by name" )
14851509 else :
14861510 logger .error (f"Could not find layout by name: { layout_name } " )
1511+ # Fall back to first available layout
1512+ layouts = find_layout_files ()
1513+ if layouts :
1514+ first_layout = layouts [0 ]
1515+ first_layout_name = os .path .basename (first_layout ).replace (".jsonc" , "" )
1516+ set_state_value ("WAYBAR_LAYOUT_PATH" , first_layout )
1517+ set_state_value ("WAYBAR_LAYOUT_NAME" , first_layout_name )
1518+ CONFIG_JSONC .parent .mkdir (parents = True , exist_ok = True )
1519+ shutil .copyfile (first_layout , CONFIG_JSONC )
1520+ logger .debug (f"Used first available layout: { first_layout } " )
1521+ else :
1522+ # No layout path in state file or layout path is empty
1523+ logger .debug ("No valid layout path in state file, determining current layout" )
1524+ current_layout = get_current_layout_from_config ()
1525+ if current_layout :
1526+ CONFIG_JSONC .parent .mkdir (parents = True , exist_ok = True )
1527+ shutil .copyfile (current_layout , CONFIG_JSONC )
1528+ logger .debug (f"Created config.jsonc from determined layout: { current_layout } " )
14871529 else :
14881530 logger .debug ("State file not found, creating it" )
14891531 ensure_state_file ()
0 commit comments