Skip to content

Case Study Picker Focus Bug

rtd edited this page Jan 12, 2026 · 1 revision

Case Study: Picker Focus Bug

Type: DEBUG Skill: code-foundations → cc-developer-character → cc-quality-practices Method: Scientific Debugging

The Problem

The picker window opens but the input field doesn't have focus.


How Code-Foundations Guided This Fix

1. Task Classification + Mindset Check

Skill(code-foundations) → classifies as DEBUG
Skill(cc-developer-character) → "understand before fixing"

The skill enforced reading 369 lines of code before forming any hypothesis.

2. Scientific Debugging Method

Instead of adding print statements or randomly trying fixes:

Step Action
Hypothesis The focus code window?.contentView?.subviews.first { $0 is NSTextField } is finding the wrong text field
Reasoning View hierarchy shows promptLabel (NSLabel) added before textField, and .first would match promptLabel
Verification Grep confirmed NSLabel: NSTextField - so NSLabel is an NSTextField
Fix Add focusInput() method on PickerWindow that directly references the correct textField

3. The Verification Step

Grep(pattern: "class NSLabel", output_mode: "content")
  Found 1 match: class NSLabel: NSTextField { ... }

This took seconds but provided certainty. Without it, the fix would have been a guess.


What Could Have Gone Wrong

Without Skill With Skill
Tried makeFirstResponder multiple times Formed hypothesis first
Added delays, forced focus in lifecycle methods Verified hypothesis before touching code
Searched for "second NSTextField" Used existing textField reference
10x longer debugging Targeted fix in minutes

The Methodology

Classify Task → Mindset Check → Read Code → Form Hypothesis → Verify → Fix

Full Session Transcript

User: The issue is when the window pops up, the input box does not have focus.
      Can we make sure it starts with focus?

Skill(code-foundations)
  Successfully loaded skill

Task Classification: DEBUG - The picker window opens but the input box
  doesn't have focus when it should.

  Let me invoke the developer character skill first, then examine the code.

Skill(cc-developer-character)
  Successfully loaded skill

Good. DEBUG task - I need to understand the code before fixing.

Read(grid-server/Sources/GridPicker/main.swift)
  Read 369 lines

I can see the issue. Let me apply the Scientific Debugging Method.

  Hypothesis: The focus code on line 346 is finding the wrong NSTextField.
  Looking at the view hierarchy:
  1. promptLabel (NSLabel, which extends NSTextField) - added first
  2. textField (the actual editable input) - added second
  3. closeButton - added third

  The code `window?.contentView?.subviews.first { $0 is NSTextField }`
  will find promptLabel first since NSLabel inherits from NSTextField.

  Verification: Let me confirm NSLabel extends NSTextField.

Grep(pattern: "class NSLabel", path: "...", output_mode: "content")
  Found 1 match: class NSLabel: NSTextField { ... }

Confirmed. NSLabel extends NSTextField on line 286, so `.first`
  matches the prompt label, not the input field.

  Fix: Add a method on PickerWindow to focus the input, since it
  already has the textField reference. This avoids fragile subview searching.

Edit(grid-server/Sources/GridPicker/main.swift)
  Added focusInput() method and updated caller

Key Takeaways

  1. Classification → Mindset → Understanding → Fix - The skill enforced this order
  2. Hypothesis-driven - "I think X is happening because Y" before touching code
  3. Verification - A quick grep confirmed the hypothesis with certainty
  4. Targeted fix - Used existing reference instead of fragile subview searching

Clone this wiki locally