duongthienz commited on
Commit
844c99d
Β·
verified Β·
1 Parent(s): da436a8

Add a reset button for rename

Browse files
Files changed (1) hide show
  1. ui.py +66 -49
ui.py CHANGED
@@ -243,69 +243,86 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
243
  input_key = f"inline_rename_input_{currFile}_{sp}"
244
  confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
245
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
 
 
246
 
247
  history = st.session_state.inline_rename_history
248
- # Read the saved rename directly from speakerRenames so that
249
- # current_val is "" when no rename exists, even if display_name
250
- # happens to equal the raw speaker label for some other reason.
251
  current_val = st.session_state.speakerRenames.get(currFile, {}).get(sp, "")
252
 
253
  _NEW_OPTION = "οΌ‹ Enter a new name"
254
 
255
  with row_cols[0]:
256
- if history:
257
- # Show dropdown of past names; last option triggers text input
258
- options = history + [_NEW_OPTION]
259
- default_idx = (
260
- history.index(current_val) if current_val in history
261
- else len(options) - 1
262
- )
263
- chosen = st.selectbox(
264
- "Rename",
265
- options=options,
266
- index=default_idx,
267
- key=select_key,
268
- label_visibility="collapsed",
269
- )
270
- # Only show text input when user wants a brand-new name
271
- if chosen == _NEW_OPTION:
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  if input_key not in st.session_state:
273
- st.session_state[input_key] = ""
274
  new_name = st.text_input(
275
- "New name",
276
  key=input_key,
277
  label_visibility="collapsed",
278
  placeholder=f"Rename {sp}…",
279
  )
280
- else:
281
- new_name = chosen
282
- st.session_state.pop(input_key, None)
283
- else:
284
- # No history yet β€” just a plain text input
285
- if input_key not in st.session_state:
286
- st.session_state[input_key] = current_val
287
- new_name = st.text_input(
288
- "Rename",
289
- key=input_key,
290
- label_visibility="collapsed",
291
- placeholder=f"Rename {sp}…",
292
- )
293
 
294
- btn_col1, btn_col2 = st.columns(2)
295
- if btn_col1.button("βœ“", key=confirm_key, help="Confirm rename"):
296
- confirmed = (new_name or "").strip()
297
- apply_inline_rename(currFile, sp, confirmed)
298
- if confirmed and confirmed not in st.session_state.inline_rename_history:
299
- st.session_state.inline_rename_history.append(confirmed)
300
- st.session_state.inline_rename_active[edit_key] = False
301
- st.session_state.pop(select_key, None)
302
- st.session_state.pop(input_key, None)
303
- st.rerun()
304
- if btn_col2.button("βœ•", key=cancel_key, help="Cancel"):
305
- st.session_state.inline_rename_active[edit_key] = False
306
- st.session_state.pop(select_key, None)
307
- st.session_state.pop(input_key, None)
308
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  else:
310
  with row_cols[0]:
311
  name_col, pencil_col = st.columns([4, 1])
 
243
  input_key = f"inline_rename_input_{currFile}_{sp}"
244
  confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
245
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
246
+ reset_key = f"inline_rename_reset_{currFile}_{sp}"
247
+ form_key = f"inline_rename_form_{currFile}_{sp}"
248
 
249
  history = st.session_state.inline_rename_history
 
 
 
250
  current_val = st.session_state.speakerRenames.get(currFile, {}).get(sp, "")
251
 
252
  _NEW_OPTION = "οΌ‹ Enter a new name"
253
 
254
  with row_cols[0]:
255
+ # Wrap in a form so pressing Enter submits (confirms) the rename
256
+ with st.form(key=form_key, border=False):
257
+ if history:
258
+ options = history + [_NEW_OPTION]
259
+ default_idx = (
260
+ history.index(current_val) if current_val in history
261
+ else len(options) - 1
262
+ )
263
+ chosen = st.selectbox(
264
+ "Rename",
265
+ options=options,
266
+ index=default_idx,
267
+ key=select_key,
268
+ label_visibility="collapsed",
269
+ )
270
+ if chosen == _NEW_OPTION:
271
+ if input_key not in st.session_state:
272
+ st.session_state[input_key] = ""
273
+ new_name = st.text_input(
274
+ "New name",
275
+ key=input_key,
276
+ label_visibility="collapsed",
277
+ placeholder=f"Rename {sp}…",
278
+ )
279
+ else:
280
+ new_name = chosen
281
+ st.session_state.pop(input_key, None)
282
+ else:
283
+ # No history yet β€” just a plain text input
284
  if input_key not in st.session_state:
285
+ st.session_state[input_key] = current_val
286
  new_name = st.text_input(
287
+ "Rename",
288
  key=input_key,
289
  label_visibility="collapsed",
290
  placeholder=f"Rename {sp}…",
291
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
+ # Right-align the three buttons: confirm > cancel > reset
294
+ st.markdown(
295
+ "<style>div[data-testid='stFormSubmitButton'] {"
296
+ "display:flex; justify-content:flex-end; gap:4px;}</style>",
297
+ unsafe_allow_html=True,
298
+ )
299
+ _, btn_col1, btn_col2, btn_col3 = st.columns([4, 1, 1, 1])
300
+ confirmed_submit = btn_col1.form_submit_button("βœ“", help="Confirm rename (or press Enter)")
301
+ cancel_clicked = btn_col2.form_submit_button("βœ•", help="Cancel")
302
+ reset_clicked = btn_col3.form_submit_button("⟳", help="Reset to original speaker label")
303
+
304
+ if confirmed_submit:
305
+ confirmed = (new_name or "").strip()
306
+ apply_inline_rename(currFile, sp, confirmed)
307
+ if confirmed and confirmed not in st.session_state.inline_rename_history:
308
+ st.session_state.inline_rename_history.append(confirmed)
309
+ st.session_state.inline_rename_active[edit_key] = False
310
+ st.session_state.pop(select_key, None)
311
+ st.session_state.pop(input_key, None)
312
+ st.rerun()
313
+ if cancel_clicked:
314
+ st.session_state.inline_rename_active[edit_key] = False
315
+ st.session_state.pop(select_key, None)
316
+ st.session_state.pop(input_key, None)
317
+ st.rerun()
318
+ if reset_clicked:
319
+ # Revert to raw speaker label β€” same as removing the token
320
+ # from a rename entry in the sidebar
321
+ apply_inline_rename(currFile, sp, "")
322
+ st.session_state.inline_rename_active[edit_key] = False
323
+ st.session_state.pop(select_key, None)
324
+ st.session_state.pop(input_key, None)
325
+ st.rerun()
326
  else:
327
  with row_cols[0]:
328
  name_col, pencil_col = st.columns([4, 1])