فهرست منبع

feat(ocr_validator): add clear selection callback for layout management

- Implemented a new `_clear_selection_callback` method to handle the clearing of selection and search states in the OCR layout manager.
- Updated the button for clearing selections to utilize the new callback, improving the user experience by ensuring proper state management.
- Enhanced the layout management functionality by ensuring that widget states are reset correctly upon user interaction.
zhch158_admin 2 روز پیش
والد
کامیت
905dcfae5d
1فایلهای تغییر یافته به همراه19 افزوده شده و 6 حذف شده
  1. 19 6
      ocr_validator/ocr_validator_layout.py

+ 19 - 6
ocr_validator/ocr_validator_layout.py

@@ -50,6 +50,19 @@ class OCRLayoutManager:
         self.show_all_boxes = False
         self.fit_to_container = False
         self.zoom_level = 1.0
+
+    def _clear_selection_callback(self, layout_type: str):
+        """在按钮回调中清理选择/搜索状态(避免 widget 实例化后修改报错)"""
+        # 业务态
+        st.session_state.selected_text = ""
+        # 紧凑布局的搜索态
+        st.session_state.compact_search_query = ""
+
+        # widget key 对应的状态(必须在 on_click 回调里改)
+        search_key = f"{layout_type}_search_input"
+        quick_select_key = f"{layout_type}_quick_text_selector"
+        st.session_state[search_key] = ""
+        st.session_state[quick_select_key] = 0
     
     def clear_image_cache(self):
         """清理所有图像缓存"""
@@ -595,12 +608,12 @@ class OCRLayoutManager:
                 st.rerun()
  
         with col5:
-            if st.button("🧹 清除选择", key=f"{layout_type}_clear_selection"):
-                # 清除选中的文本
-                st.session_state.selected_text = None
-                # 清除搜索框内容
-                st.session_state.compact_search_query = None
-                st.rerun()
+            st.button(
+                "🧹 清除选择",
+                key=f"{layout_type}_clear_selection",
+                on_click=self._clear_selection_callback,
+                kwargs={"layout_type": layout_type},
+            )
 
         # 使用增强的图像加载方法
         image = self.load_and_rotate_image(self.validator.image_path)