# FileChooser

`com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser`

Browses native Java files with optional selection and edit callbacks. Construct the overlay, set `root` before opening when needed, then open it through its parent to initialize its controls. The root is a navigation reference, not a filesystem boundary; relative paths may escape it.  
 ```js
function openFileChooser(parent, font, directory, onSelect, onEdit) {
  const FileChooser = Java.type('com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser');  
  const chooser = new FileChooser(10, 10, 320, 180, font, directory, null, parent,  
    JavaWrapper.methodToJava(onSelect), JavaWrapper.methodToJava(onEdit));  
  chooser.root = directory;  
  parent.openOverlay(chooser);  
  return chooser;  
}
 ```

 The caller supplies a listable native Java directory, native font, overlay parent and JavaScript callback functions.

## Nested types

- [fileObj](FileChooser.fileObj.md)
- [sortFile](FileChooser.sortFile.md)

## Constructors

### new FileChooser {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_constructor-int-int-int-int-Font-File-File-IOverlayParent-Consumer-Consumer-}

```js
new FileChooser(int x, int y, int width, int height, Font textRenderer, File directory, File selected, IOverlayParent parent, Consumer<File> setFile, Consumer<File> editFile)
```

Stores the overlay geometry, browser state and callbacks; controls are registered when the parent opens and initializes this overlay.

**Parameters:**
- `x`: left coordinate in GUI space
- `y`: top coordinate in GUI space
- `width`: width in GUI units
- `height`: height in GUI units
- `textRenderer`: non-null native font for the controls
- `directory`: non-null initial browse directory, listable or creatable during initialization
- `selected`: nullable file initially selected during initialization
- `parent`: non-null overlay host
- `setFile`: nullable callback invoked by the Select button when a selection is stored; null makes Select do nothing, and the overlay closes only after a normal callback return
- `editFile`: callback invoked by the Edit button with the stored selection; it is not null-checked and does not close the overlay

## Fields

### root {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_root}

```js
root: File
```

Expected non-null navigation base used for the displayed relative path, parent-row boundary and failed-directory-creation fallback; initially the configured macro folder. It is not a security boundary, and changing it does not itself navigate or refresh the listing.

## Methods

### addFile {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_addFile-File-}

```js
addFile(File f): void
```

Adds a row using the file's name as its literal button label. Requires initialized controls.

**Parameters:**
- `f`: non-null native file or directory represented by the row

### addFile {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_addFile-File-String-}

```js
addFile(File f, String btnText): void
```

Appends and registers a row with the supplied literal label, using five columns and twelve-GUI-unit rows. Clicking it calls `selectFile`; duplicate entries are not filtered.  
 Requires an initialized scrollbar: the row is appended and registered before the scrollbar page-count update, so failures can leave partial effects.  
 The page-count update can change scrollbar state and notify its callback, repositioning rows.

**Parameters:**
- `f`: file or directory represented by the row
- `btnText`: literal button label

### confirmDelete {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_confirmDelete-FileChooser.fileObj-}

```js
confirmDelete(FileChooser.fileObj f): void
```

Opens a confirmation overlay that calls `delete(f)` only when confirmed; it does not delete immediately.  
 Captures the mutable row record, not a snapshot of its fields. This method does not verify listing membership or require a regular file.

**Parameters:**
- `f`: row record captured by the confirmation callback

### delete {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_delete-FileChooser.fileObj-}

```js
delete(FileChooser.fileObj f): void
```

Removes the row from tracking and the parent, then attempts `File.delete()` and repositions remaining rows.  
 The delete result is ignored: a false result does not restore the row. Unchecked failures interrupt the remaining steps without rollback.  
 Does not confirm, verify membership, require a regular file or recursively delete a directory. Selection and scrollbar page count are not updated.

**Parameters:**
- `f`: row record whose button and file are used

### extractRenderState {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_extractRenderState-GuiGraphicsExtractor-int-int-float-}

```js
extractRenderState(GuiGraphicsExtractor drawContext, int mouseX, int mouseY, float delta): void
```

Submits this chooser's background, directory heading and separators, then delegates widget and child-overlay rendering before submitting popups for truncated hovered buttons. Popup eligibility uses each button's stored hover state and text-fit state; it does not check visibility or active state, and multiple buttons can qualify. Call after initialization so the directory heading is set.

**Parameters:**
- `drawContext`: graphics context receiving the drawing operations
- `mouseX`: GUI-space mouse x-coordinate forwarded to widgets and used for popup placement
- `mouseY`: GUI-space mouse y-coordinate forwarded to widgets and used for popup placement
- `delta`: frame value forwarded to the superclass

### init {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_init-}

```js
init(): void
```

Clears local tracking, registers new controls and a scrollbar, resets the row origin to `y + 13`, then rebuilds the directory listing.  
 Opening through the parent calls this. The listing rebuild separately removes old file rows from the parent, but repeated initialization leaves prior non-row controls registered.  
 A non-null initial selection is passed to `selectFile`, so a directory selection can navigate again; initialization does not notify the selection or edit consumers.  
 Filesystem failures can leave partially initialized controls.

### onScrollbar {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_onScrollbar-double-}

```js
onScrollbar(double page): void
```

Sets the row origin to `y + 13 - (int) (page * (height - 27))` and repositions rows on every call.  
 The page argument is not clamped; this changes row positions and visibility, not active flags, directory contents, page count or the scrollbar thumb.

**Parameters:**
- `page`: scrollbar amount in multiples of `height - 27`

### selectFile {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_selectFile-File-}

```js
selectFile(File f): void
```

Navigates into a directory without clearing the stored file selection, or stores a non-directory file as the selection, then updates row highlighting against the supplied file.  
 Does not require a listed or existing file, invoke either callback, confirm a choice or close the overlay.

**Parameters:**
- `f`: file or directory to navigate to or select

### setDir {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_setDir-File-}

```js
setDir(File dir): void
```

Rebuilds the listing for `dir`, attempting to create it when absent. Requires an initialized scrollbar and usable root; old rows are removed before filesystem checks.  
 Failed creation recursively attempts `root`; there is no cycle guard if that directory cannot be created either.  
 The heading is relative to the absolute root path, not a canonical containment check. Entries are sorted directories first, then by name.  
 A null directory listing is not treated as empty; unchecked filesystem or path failures can leave a partial rebuild.  
 Existing selection is retained. Row additions update scrollbar page count, which can reset the offset through its callback.

**Parameters:**
- `dir`: non-null directory to browse, listable or creatable and with a path compatible with the root for relativization

### updateFilePos {#com.jsmacrosce.jsmacros.client.gui.overlays.FileChooser_updateFilePos-}

```js
updateFilePos(): void
```

Repositions and resizes existing rows in five columns of twelve-GUI-unit rows using current geometry and scroll position.  
 A row is visible when its top lies between `y + 13` and `y + height - 27`, inclusive.  
 Does not change active flags, create controls, rebuild the listing or update scrollbar page count.
