Downloads component now uses textarea widget to display file preview.

This commit is contained in:
Jakob Friedl
2025-10-15 12:22:38 +02:00
parent 1e95b67603
commit 80579e5c7f
2 changed files with 18 additions and 5 deletions

View File

@@ -3,12 +3,14 @@ import imguin/[cimgui, glfw_opengl, simple]
import ../../utils/[appImGui, colors]
import ../../../common/[types, utils]
import ../../core/websocket
import ../widgets/textarea
type
DownloadsComponent* = ref object of RootObj
title: string
items*: seq[LootItem]
contents*: Table[string, string]
textarea: TextareaWidget
selectedIndex: int
@@ -18,6 +20,7 @@ proc LootDownloads*(title: string): DownloadsComponent =
result.items = @[]
result.contents = initTable[string, string]()
result.selectedIndex = -1
result.textarea = Textarea(showTimestamps = false, autoScroll = false)
proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: WsConnection) =
igBegin(component.title, showComponent, 0)
@@ -64,6 +67,7 @@ proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: W
let isSelected = component.selectedIndex == i
if igSelectable_Bool(item.lootId.cstring, isSelected, ImGuiSelectableFlags_SpanAllColumns.int32 or ImGuiSelectableFlags_AllowOverlap.int32, vec2(0, 0)):
component.selectedIndex = i
component.textarea.clear()
if igIsItemHovered(ImGuiHoveredFlags_None.int32) and igIsMouseClicked_Bool(ImGuiMouseButton_Right.int32, false):
component.selectedIndex = i
@@ -132,8 +136,12 @@ proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: W
igSeparator()
igDummy(vec2(0.0f, 5.0f))
igTextUnformatted(component.contents[item.lootId], nil)
if component.textarea.isEmpty() and not component.contents[item.lootId].isEmptyOrWhitespace():
component.textarea.addItem(LOG_OUTPUT, component.contents[item.lootId])
component.textarea.draw(vec2(-1.0f, -1.0f))
else:
igText("Select item to preview contents")
igEndChild()

View File

@@ -9,6 +9,7 @@ type
contentDisplayed: ConsoleItems
textSelect: ptr TextSelect
showTimestamps: bool
autoScroll: bool
# Text highlighting
proc getText(item: ConsoleItem): cstring =
@@ -32,7 +33,7 @@ proc getLineAtIndex(i: csize_t, data: pointer, outLen: ptr csize_t): cstring {.c
outLen[] = line.len.csize_t
return line
proc Textarea*(showTimestamps: bool = true): TextareaWidget =
proc Textarea*(showTimestamps: bool = true, autoScroll: bool = true): TextareaWidget =
result = new TextareaWidget
result.content = new ConsoleItems
result.content.items = @[]
@@ -40,6 +41,7 @@ proc Textarea*(showTimestamps: bool = true): TextareaWidget =
result.contentDisplayed.items = @[]
result.textSelect = textselect_create(getLineAtIndex, getNumLines, cast[pointer](result.contentDisplayed), 0)
result.showTimestamps = showTimestamps
result.autoScroll = autoScroll
# API to add new content entry
proc addItem*(component: TextareaWidget, itemType: LogType, data: string, timestamp: string = now().format("dd-MM-yyyy HH:mm:ss")) =
@@ -55,9 +57,11 @@ proc clear*(component: TextareaWidget) =
component.contentDisplayed.items.setLen(0)
component.textSelect.textselect_clear_selection()
proc isEmpty*(component: TextareaWidget): bool =
return component.content.items.len() <= 0
# Drawing
proc print(component: TextareaWidget, item: ConsoleItem) =
if item.itemType != LOG_OUTPUT and component.showTimestamps:
igTextColored(GRAY, "[" & item.timestamp & "]", nil)
igSameLine(0.0f, 0.0f)
@@ -103,8 +107,9 @@ proc draw*(component: TextareaWidget, size: ImVec2, filter: ptr ImGuiTextFilter
component.print(item)
# Auto-scroll to bottom
if igGetScrollY() >= igGetScrollMaxY():
igSetScrollHereY(1.0f)
if component.autoScroll:
if igGetScrollY() >= igGetScrollMaxY():
igSetScrollHereY(1.0f)
component.textSelect.textselect_update()