duongthienz commited on
Commit
5b6254c
·
verified ·
1 Parent(s): ad7eece

Update edit function for Rename Speaker

Browse files
Files changed (1) hide show
  1. ui.py +41 -21
ui.py CHANGED
@@ -145,15 +145,13 @@ def render_data_table(tableDF, speakerNames, raw_to_display, currFile):
145
  # ---------------------------------------------------------------------------
146
 
147
  def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
148
- """Render a table: Speaker (with inline ✎ rename + history suggestions) | Audio Sample | ↺ button."""
149
  file_samples = st.session_state.speakerClips.get(currFile, {})
150
  has_waveform = currFile in st.session_state.speakerWaveforms
151
  has_samples = bool(file_samples)
152
 
153
- # Per-speaker editing toggle: {(currFile, sp): True}
154
  if "inline_rename_active" not in st.session_state:
155
  st.session_state.inline_rename_active = {}
156
- # Ordered list of all confirmed rename names (deduped, insertion order)
157
  if "inline_rename_history" not in st.session_state:
158
  st.session_state.inline_rename_history = []
159
 
@@ -179,38 +177,60 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
179
  input_key = f"inline_rename_input_{currFile}_{sp}"
180
  confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
181
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
182
- fill_prefix = f"inline_rename_fill_{currFile}_{sp}"
 
 
183
 
184
  with row_cols[0]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  new_name = st.text_input(
186
  "Rename",
187
- value=display_name if display_name != sp else "",
188
  key=input_key,
189
  label_visibility="collapsed",
190
  placeholder=f"Rename {sp}…",
191
  )
192
 
193
- # Clickable history suggestions
194
- history = st.session_state.inline_rename_history
195
- if history:
196
- st.caption("Quick fill:")
197
- fill_cols = st.columns(len(history))
198
- for hi, past_name in enumerate(history):
199
- if fill_cols[hi].button(
200
- past_name,
201
- key=f"{fill_prefix}_{hi}",
202
- use_container_width=True,
203
- ):
204
- apply_inline_rename(currFile, sp, past_name)
205
- st.session_state.inline_rename_active[edit_key] = False
206
- st.rerun()
207
-
208
  btn_col1, btn_col2 = st.columns(2)
209
  if btn_col1.button("✓", key=confirm_key, help="Confirm rename"):
210
  confirmed = new_name.strip()
211
  if confirmed:
212
  apply_inline_rename(currFile, sp, confirmed)
213
- # Add to history if not already present
214
  if confirmed not in st.session_state.inline_rename_history:
215
  st.session_state.inline_rename_history.append(confirmed)
216
  st.session_state.inline_rename_active[edit_key] = False
 
145
  # ---------------------------------------------------------------------------
146
 
147
  def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
148
+ """Render a table: Speaker (with inline ✎ rename + scrollable dropdown history) | Audio Sample | ↺ button."""
149
  file_samples = st.session_state.speakerClips.get(currFile, {})
150
  has_waveform = currFile in st.session_state.speakerWaveforms
151
  has_samples = bool(file_samples)
152
 
 
153
  if "inline_rename_active" not in st.session_state:
154
  st.session_state.inline_rename_active = {}
 
155
  if "inline_rename_history" not in st.session_state:
156
  st.session_state.inline_rename_history = []
157
 
 
177
  input_key = f"inline_rename_input_{currFile}_{sp}"
178
  confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
179
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
180
+
181
+ history = st.session_state.inline_rename_history
182
+ current_val = display_name if display_name != sp else ""
183
 
184
  with row_cols[0]:
185
+ # Render a native <datalist> above the Streamlit input.
186
+ # The browser connects them by matching the input's list= attribute,
187
+ # which we set on the nearest text input via scoped JS.
188
+ if history:
189
+ dl_id = f"dl_{sp}_{currFile}".replace(" ", "_").replace(".", "_")
190
+ options = "\n".join(f'<option value="{n}"></option>' for n in history)
191
+ st.markdown(
192
+ f"""
193
+ <datalist id="{dl_id}">{options}</datalist>
194
+ <style>
195
+ /* Make the native dropdown scrollable at ~160px */
196
+ #{dl_id} {{ max-height: 160px; overflow-y: auto; }}
197
+ </style>
198
+ <script>
199
+ (function() {{
200
+ // Wait one tick for Streamlit to render the input,
201
+ // then attach the datalist to the very next text input
202
+ // that appears after this script tag.
203
+ var dl = document.getElementById('{dl_id}');
204
+ var observer = new MutationObserver(function() {{
205
+ var inputs = document.querySelectorAll('input[data-testid="stTextInput"]');
206
+ if (inputs.length) {{
207
+ inputs[inputs.length - 1].setAttribute('list', '{dl_id}');
208
+ observer.disconnect();
209
+ }}
210
+ }});
211
+ observer.observe(document.body, {{ childList: true, subtree: true }});
212
+ // Also try immediately
213
+ var inputs = document.querySelectorAll('input[data-testid="stTextInput"]');
214
+ if (inputs.length) inputs[inputs.length - 1].setAttribute('list', '{dl_id}');
215
+ }})();
216
+ </script>
217
+ """,
218
+ unsafe_allow_html=True,
219
+ )
220
+
221
  new_name = st.text_input(
222
  "Rename",
223
+ value=current_val,
224
  key=input_key,
225
  label_visibility="collapsed",
226
  placeholder=f"Rename {sp}…",
227
  )
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  btn_col1, btn_col2 = st.columns(2)
230
  if btn_col1.button("✓", key=confirm_key, help="Confirm rename"):
231
  confirmed = new_name.strip()
232
  if confirmed:
233
  apply_inline_rename(currFile, sp, confirmed)
 
234
  if confirmed not in st.session_state.inline_rename_history:
235
  st.session_state.inline_rename_history.append(confirmed)
236
  st.session_state.inline_rename_active[edit_key] = False