18
18
from textual .containers import VerticalScroll
19
19
from textual .css .query import QueryError
20
20
21
+ from tldw_chatbook .Event_Handlers .Chat_Events .chat_events_sidebar import (
22
+ handle_chat_media_load_selected_button_pressed , handle_chat_media_copy_title_button_pressed ,
23
+ handle_chat_media_copy_content_button_pressed , handle_chat_media_copy_author_button_pressed ,
24
+ handle_chat_media_copy_url_button_pressed )
21
25
from tldw_chatbook .Utils .Utils import safe_float , safe_int
22
26
#
23
27
# Local Imports
41
45
#
42
46
# Functions:
43
47
44
- async def handle_chat_tab_sidebar_toggle (app : 'TldwCli' , button_id : str ) -> None :
48
+ async def handle_chat_tab_sidebar_toggle (app : 'TldwCli' , event : Button . Pressed ) -> None :
45
49
"""Handles sidebar toggles specific to the Chat tab."""
46
50
logger = getattr (app , 'loguru_logger' , logging )
51
+ button_id = event .button .id
47
52
if button_id == "toggle-chat-left-sidebar" :
48
53
app .chat_sidebar_collapsed = not app .chat_sidebar_collapsed
49
54
logger .debug ("Chat tab settings sidebar (left) now %s" , "collapsed" if app .chat_sidebar_collapsed else "expanded" )
@@ -53,8 +58,9 @@ async def handle_chat_tab_sidebar_toggle(app: 'TldwCli', button_id: str) -> None
53
58
else :
54
59
logger .warning (f"Unhandled sidebar toggle button ID '{ button_id } ' in Chat tab handler." )
55
60
56
- async def handle_chat_send_button_pressed (app : 'TldwCli' , prefix : str ) -> None :
61
+ async def handle_chat_send_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
57
62
"""Handles the send button press for the main chat tab."""
63
+ prefix = "chat" # This handler is specific to the main chat tab's send button
58
64
loguru_logger .info (f"Send button pressed for '{ prefix } ' (main chat)" ) # Use loguru_logger consistently
59
65
60
66
# --- 1. Query UI Widgets ---
@@ -766,7 +772,7 @@ async def handle_chat_action_button_pressed(app: 'TldwCli', button: Button, acti
766
772
description = f"Regenerating for { selected_provider_regen } " )
767
773
768
774
769
- async def handle_chat_new_conversation_button_pressed (app : 'TldwCli' ) -> None :
775
+ async def handle_chat_new_conversation_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
770
776
loguru_logger .info ("New Chat button pressed." )
771
777
try :
772
778
chat_log_widget = app .query_one ("#chat-log" , VerticalScroll )
@@ -808,7 +814,7 @@ async def handle_chat_new_conversation_button_pressed(app: 'TldwCli') -> None:
808
814
loguru_logger .error (f"UI component not found during new chat setup: { e } " )
809
815
810
816
811
- async def handle_chat_save_current_chat_button_pressed (app : 'TldwCli' ) -> None :
817
+ async def handle_chat_save_current_chat_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
812
818
loguru_logger .info ("Save Current Chat button pressed." )
813
819
if not (app .current_chat_is_ephemeral and app .current_chat_conversation_id is None ):
814
820
loguru_logger .warning ("Chat not eligible for saving (not ephemeral or already has ID)." )
@@ -905,7 +911,7 @@ async def handle_chat_save_current_chat_button_pressed(app: 'TldwCli') -> None:
905
911
app .notify (f"Error saving chat: { str (e_save_chat )[:100 ]} " , severity = "error" )
906
912
907
913
908
- async def handle_chat_save_details_button_pressed (app : 'TldwCli' ) -> None :
914
+ async def handle_chat_save_details_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
909
915
loguru_logger .info ("Save conversation details button pressed." )
910
916
if app .current_chat_is_ephemeral or not app .current_chat_conversation_id :
911
917
loguru_logger .warning ("Cannot save details for an ephemeral or non-existent chat." )
@@ -1009,7 +1015,7 @@ async def handle_chat_save_details_button_pressed(app: 'TldwCli') -> None:
1009
1015
app .notify ("Unexpected error saving details." , severity = "error" , timeout = 3 )
1010
1016
1011
1017
1012
- async def handle_chat_load_selected_button_pressed (app : 'TldwCli' ) -> None :
1018
+ async def handle_chat_load_selected_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
1013
1019
loguru_logger .info ("Load selected chat button pressed." )
1014
1020
try :
1015
1021
results_list_view = app .query_one ("#chat-conversation-search-results-list" , ListView )
@@ -1048,7 +1054,7 @@ async def handle_chat_load_selected_button_pressed(app: 'TldwCli') -> None:
1048
1054
app .notify ("Unexpected error loading chat." , severity = "error" )
1049
1055
1050
1056
1051
- async def perform_chat_conversation_search (app : 'TldwCli' ) -> None :
1057
+ async def perform_chat_conversation_search (app : 'TldwCli' , event : Button . Pressed ) -> None :
1052
1058
loguru_logger .debug ("Performing chat conversation search..." )
1053
1059
try :
1054
1060
search_bar = app .query_one ("#chat-conversation-search-bar" , Input )
@@ -1294,7 +1300,7 @@ async def display_conversation_in_chat_tab_ui(app: 'TldwCli', conversation_id: s
1294
1300
loguru_logger .info (f"Displayed conversation '{ conv_metadata .get ('title' , 'Untitled' )} ' (ID: { conversation_id } ) in chat tab." )
1295
1301
1296
1302
1297
- async def load_branched_conversation_history_ui (app : 'TldwCli' , target_conversation_id : str , chat_log_widget : VerticalScroll ):
1303
+ async def load_branched_conversation_history_ui (app : 'TldwCli' , target_conversation_id : str , chat_log_widget : VerticalScroll ) -> None :
1298
1304
"""
1299
1305
Loads the complete message history for a given conversation_id,
1300
1306
tracing back through parent branches to the root if necessary.
@@ -1605,7 +1611,7 @@ async def handle_chat_character_attribute_changed(app: 'TldwCli', event: Union[I
1605
1611
loguru_logger .warning (f"Attribute change event from unmapped control_id: { control_id } " )
1606
1612
1607
1613
1608
- async def handle_chat_clear_active_character_button_pressed (app : 'TldwCli' ) -> None :
1614
+ async def handle_chat_clear_active_character_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
1609
1615
"""Clears the currently active character data and resets related UI fields."""
1610
1616
loguru_logger .info ("Clear Active Character button pressed." )
1611
1617
@@ -1732,7 +1738,7 @@ async def perform_chat_prompt_search(app: 'TldwCli') -> None:
1732
1738
logger .error (f"Chat Tab: Error performing prompt search via perform_chat_prompt_search: { e } " , exc_info = True )
1733
1739
1734
1740
1735
- async def handle_chat_view_selected_prompt_button_pressed (app : 'TldwCli' ) -> None :
1741
+ async def handle_chat_view_selected_prompt_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
1736
1742
logger = getattr (app , 'loguru_logger' , logging )
1737
1743
logger .debug ("Chat Tab: View Selected Prompt button pressed." )
1738
1744
@@ -1977,7 +1983,7 @@ async def handle_chat_sidebar_prompt_search_changed(
1977
1983
logger .info (f"[Prompts] Search '{ search_term } ' → { len (prompts )} results." )
1978
1984
1979
1985
1980
- async def handle_continue_response_button_pressed (app : 'TldwCli' , button : Button , message_widget : ChatMessage ) -> None :
1986
+ async def handle_continue_response_button_pressed (app : 'TldwCli' , event : Button . Pressed , message_widget : ChatMessage ) -> None :
1981
1987
"""Handles the 'Continue Response' button press on an AI chat message."""
1982
1988
loguru_logger .info (f"Continue Response button pressed for message_id: { message_widget .message_id_internal } , current text: '{ message_widget .message_text [:50 ]} ...'" )
1983
1989
db = app .chachanotes_db
@@ -1989,9 +1995,8 @@ async def handle_continue_response_button_pressed(app: 'TldwCli', button: Button
1989
1995
original_display_text_obj : Optional [Union [str , Text ]] = None # renderable can be str or Text
1990
1996
1991
1997
try :
1992
- # Ensure we are targeting the correct button on the specific message_widget instance
1993
- # The 'button' argument is the button that was pressed.
1994
- continue_button_widget = button # This IS the button that was pressed.
1998
+ button = event .button
1999
+ continue_button_widget = button
1995
2000
original_button_label = continue_button_widget .label
1996
2001
continue_button_widget .disabled = True
1997
2002
continue_button_widget .label = get_char (EMOJI_THINKING , FALLBACK_THINKING ) # "⏳" or similar
@@ -2307,7 +2312,7 @@ async def handle_continue_response_button_pressed(app: 'TldwCli', button: Button
2307
2312
loguru_logger .info (f"Continuation process completed for message_id: { message_widget .message_id_internal } . Final text length: { len (current_full_text )} " )
2308
2313
2309
2314
2310
- async def handle_respond_for_me_button_pressed (app : 'TldwCli' ) -> None :
2315
+ async def handle_respond_for_me_button_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
2311
2316
"""Handles the 'Respond for Me' (Suggest) button press in the chat input area."""
2312
2317
loguru_logger .info ("Enter: handle_respond_for_me_button_pressed" )
2313
2318
loguru_logger .info ("Respond for Me button pressed." )
@@ -2541,7 +2546,7 @@ class ApiKeyMissingError(Exception): # Custom exception for cleaner handling in
2541
2546
pass
2542
2547
2543
2548
2544
- async def handle_stop_chat_generation_pressed (app : 'TldwCli' ) -> None :
2549
+ async def handle_stop_chat_generation_pressed (app : 'TldwCli' , event : Button . Pressed ) -> None :
2545
2550
"""Handles the 'Stop Chat Generation' button press."""
2546
2551
loguru_logger .info ("Stop Chat Generation button pressed." )
2547
2552
@@ -2637,11 +2642,10 @@ async def populate_chat_conversation_character_filter_select(app: 'TldwCli') ->
2637
2642
# --- Button Handler Map ---
2638
2643
# This maps button IDs to their async handler functions.
2639
2644
CHAT_BUTTON_HANDLERS = {
2640
- "send-chat" : lambda app : handle_chat_send_button_pressed ( app , "chat" ) ,
2645
+ "send-chat" : handle_chat_send_button_pressed ,
2641
2646
"respond-for-me-button" : handle_respond_for_me_button_pressed ,
2642
2647
"stop-chat-generation" : handle_stop_chat_generation_pressed ,
2643
2648
"chat-new-conversation-button" : handle_chat_new_conversation_button_pressed ,
2644
- "chat-new-temp-chat-button" : handle_chat_new_conversation_button_pressed , # Reuses handler
2645
2649
"chat-save-current-chat-button" : handle_chat_save_current_chat_button_pressed ,
2646
2650
"chat-save-conversation-details-button" : handle_chat_save_details_button_pressed ,
2647
2651
"chat-conversation-load-selected-button" : handle_chat_load_selected_button_pressed ,
@@ -2650,19 +2654,13 @@ async def populate_chat_conversation_character_filter_select(app: 'TldwCli') ->
2650
2654
"chat-prompt-copy-user-button" : handle_chat_copy_user_prompt_button_pressed ,
2651
2655
"chat-load-character-button" : handle_chat_load_character_button_pressed ,
2652
2656
"chat-clear-active-character-button" : handle_chat_clear_active_character_button_pressed ,
2653
-
2654
- # --- Sidebar Toggles ---
2655
- "toggle-chat-left-sidebar" : lambda app : handle_chat_tab_sidebar_toggle (app , "toggle-chat-left-sidebar" ),
2656
- "toggle-chat-right-sidebar" : lambda app : handle_chat_tab_sidebar_toggle (app , "toggle-chat-right-sidebar" ),
2657
-
2658
- # --- Sidebar Media Buttons (from chat_events_sidebar.py) ---
2659
- "chat-media-load-selected-button" : 'chat_events_sidebar.handle_chat_media_load_selected_button_pressed' ,
2660
- "chat-media-copy-title-button" : 'chat_events_sidebar.handle_chat_media_copy_title_button_pressed' ,
2661
- "chat-media-copy-content-button" : 'chat_events_sidebar.handle_chat_media_copy_content_button_pressed' ,
2662
- "chat-media-copy-author-button" : 'chat_events_sidebar.handle_chat_media_copy_author_button_pressed' ,
2663
- "chat-media-copy-url-button" : 'chat_events_sidebar.handle_chat_media_copy_url_button_pressed' ,
2664
-
2665
- # --- Note: ChatMessage action buttons (like edit, copy, delete) are handled separately by _get_chat_message_widget_from_button ---
2657
+ "toggle-chat-left-sidebar" : handle_chat_tab_sidebar_toggle ,
2658
+ "toggle-chat-right-sidebar" : handle_chat_tab_sidebar_toggle ,
2659
+ "chat-media-load-selected-button" : handle_chat_media_load_selected_button_pressed ,
2660
+ "chat-media-copy-title-button" : handle_chat_media_copy_title_button_pressed ,
2661
+ "chat-media-copy-content-button" : handle_chat_media_copy_content_button_pressed ,
2662
+ "chat-media-copy-author-button" : handle_chat_media_copy_author_button_pressed ,
2663
+ "chat-media-copy-url-button" : handle_chat_media_copy_url_button_pressed ,
2666
2664
}
2667
2665
2668
2666
#
0 commit comments