This commit is contained in:
Santiago Lema 2025-05-10 04:48:28 -03:00
parent 5360c4f8d0
commit 9c040edca1
5 changed files with 187 additions and 158 deletions

View file

@ -5,7 +5,6 @@
#include "MainWindow.h"
static int progressAnim = 0;
static int progressColor = 0;
static bool progressColorUp = false;
@ -14,6 +13,7 @@ static bool progressColorUp = false;
#include <Bitmap.h>
#include <Button.h>
#include <Catalog.h>
#include <IconUtils.h>
#include <LayoutBuilder.h>
#include <Menu.h>
#include <MenuBar.h>
@ -30,13 +30,13 @@ static bool progressColorUp = false;
#include <Path.h>
#include "Conversation.h"
#include <FindDirectory.h>
#include <FindDirectory.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Window"
MainWindow::MainWindow()
: BWindow(BRect(50, 50, 600, 400), B_TRANSLATE("DumBer"), B_TITLED_WINDOW,
: BWindow(BRect(50, 50, 600, 400), B_TRANSLATE("DumBer"), B_DOCUMENT_WINDOW,
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) {
// Without this conversation would never get bmessages from HttpRequest
@ -63,7 +63,6 @@ MainWindow::MainWindow()
_progress->SetTo(0);
_progress->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
// Info view, only one line high
_infoView = new BTextView("info");
_infoView->SetText("...");
@ -95,7 +94,6 @@ MainWindow::MainWindow()
_sendButton->MakeDefault(true);
_answerView = new BTextView("answer", B_WILL_DRAW);
_answerView->MakeEditable(false); // Disable editing
_answerView->MakeSelectable(true); // Enable text selection
@ -103,95 +101,126 @@ MainWindow::MainWindow()
_answerView->SetExplicitMinSize(BSize(B_SIZE_UNSET, askH * 2));
_answerScrollView =
new BScrollView("scroll_view", _answerView, B_FRAME_EVENTS | B_WILL_DRAW, 0,
false, true, B_FANCY_BORDER); // horizontal and vertical scrollbars
_answerScrollView = new BScrollView(
"scroll_view", _answerView, B_FRAME_EVENTS | B_WILL_DRAW, 0, false, true,
B_FANCY_BORDER); // horizontal and vertical scrollbars
// Enable correct resizing behavior, otherwise we get no correct scrollbar after resizing
_answerView->SetFlags(_answerView->Flags() | B_FULL_UPDATE_ON_RESIZE);
_answerScrollView->SetFlags(_answerView->Flags() | B_FULL_UPDATE_ON_RESIZE);
// Enable correct resizing behavior, otherwise we get no correct scrollbar
// after resizing
_answerView->SetFlags(_answerView->Flags() | B_FULL_UPDATE_ON_RESIZE);
_answerScrollView->SetFlags(_answerView->Flags() | B_FULL_UPDATE_ON_RESIZE);
/*
BView *imageView = new BView("icon_view", B_WILL_DRAW | B_FOLLOW_NONE);
imageView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
//TRYING TO load image from resources and failing...
BView *imageView = new BView("icon_view", B_WILL_DRAW | B_FOLLOW_ALL);
imageView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
BBitmap* oneImage = BTranslationUtils::GetBitmap('RAWT', 77, NULL);
imageView->SetViewColor(ui_color(B_CONTROL_BORDER_COLOR));
// Open the resource file
BFile resourceFile("Resources.rdef", B_READ_ONLY);
if (resourceFile.InitCheck() != B_OK) {
fprintf(stderr, "Failed to open the resource file.\n");
return;
}
if (oneImage) {
imageView->SetViewBitmap(oneImage,B_FOLLOW_LEFT_TOP, B_TILE_BITMAP);
printf("Image loaded!\n");
}
else {
printf("Image NOT loaded!\n");
}
*/
BResources resources(&resourceFile);
// Load the icon resource
size_t theSize;
const uint8 *iconData =
(const uint8 *)resources.LoadResource('VICN', 55, &theSize);
if (iconData == nullptr) {
fprintf(stderr, "Failed to load the icon resource.\n");
} else {
printf("loaded size:%zu", theSize);
}
// Create a BBitmap
BBitmap *bitmap =
new BBitmap(BRect(0, 0, 96, 96), B_RGBA32); // Adjust size as needed
if (bitmap == nullptr) {
fprintf(stderr, "Failed to create a BBitmap.\n");
}
// Convert the icon data to the Bitmap
if (!BIconUtils::GetVectorIcon(iconData, theSize, bitmap)) {
fprintf(stderr, "Failed to convert the icon to a BBitmap.\n");
// delete bitmap; // Clean up in case of failure
}
// Now you can use the bitmap as needed...
// delete bitmap; // Clean up when done
*/
BStringView *headerQuestion =
new BStringView("questionLabel", "Your question: ");
BStringView *headerAnswer = new BStringView("questionAnswer", "Answer: ");
rgb_color colorQuestion = {100, 100,150, 255};
//rgb_color colorAnswer = {100, 100,150, 255};
rgb_color colorQuestion = {100, 100, 150, 255};
// rgb_color colorAnswer = {100, 100,150, 255};
//high color = txt
headerQuestion->SetHighColor(colorQuestion);
headerAnswer->SetHighColor(colorQuestion);
// high color = txt
headerQuestion->SetHighColor(colorQuestion);
headerAnswer->SetHighColor(colorQuestion);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.AddGlue(0.1)
.Add(headerQuestion)
.AddGroup(B_HORIZONTAL, 0, 1)
.AddGlue(0.1)
.Add(headerQuestion)
.AddGroup(B_HORIZONTAL, 0, 1)
.Add(_inputField)
.AddGlue(0.01)
// .Add(imageView)
.Add(_inputField)
.AddGlue(0.01)
// .Add(imageView)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 0)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 0)
.AddGroup(B_HORIZONTAL, 0)
.Add(_infoConversation)
.AddGlue(0.1)
.End()
.AddGroup(B_HORIZONTAL, 0)
.Add(_modelField)
.AddGlue(0.1)
.End()
.AddGroup(B_HORIZONTAL, 0)
.Add(_sendButton)
.AddGlue(0.1)
.End()
.AddGroup(B_HORIZONTAL, 0)
.Add(_infoConversation)
.AddGlue(0.1)
.End()
.AddGroup(B_HORIZONTAL, 0)
.Add(_modelField)
.AddGlue(0.1)
.End()
.AddGroup(B_HORIZONTAL, 0)
.Add(_sendButton)
.AddGlue(0.1)
.End()
.End()
.End()
.End()
.End()
.AddGlue(0.1)
.Add(headerAnswer)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 1.0)
.AddGlue(0.1)
.Add(headerAnswer)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 1.0)
.Add(_answerScrollView, 1)
.Add(_progress, 0.1)
.Add(_infoView, 0.1)
.End()
.SetInsets(6, 6, 6, 6)
.Add(_answerScrollView, 1)
.Add(_progress, 0.1)
.Add(_infoView, 0.1)
.End()
.SetInsets(6, 6, 6, 6)
.End();
.End();
// Loop Just to animate progress in Bar
BMessageRunner *runner = new BMessageRunner(this, // target BHandler
new BMessage(kPulse),
100000 // interval in μs (0ms)
//imageView->SetViewColor(ui_color(B_CONTROL_BORDER_COLOR));
);
//if (bitmap) {
// imageView->SetViewBitmap(bitmap, 0, 0);
// printf("Image loaded!\n");
//} else {
// printf("Image NOT loaded!\n");
//}
updateHistoryInfo();
// Loop Just to animate progress in Bar
BMessageRunner *runner = new BMessageRunner(this, // target BHandler
new BMessage(kPulse),
100000 // interval in μs (0ms)
);
PostMessage(kCheckKey);
updateHistoryInfo();
PostMessage(kCheckKey);
}
void MainWindow::checkValidKey() {
@ -377,9 +406,9 @@ void MainWindow::MessageReceived(BMessage *message) {
case kViewJSON: {
_infoView->SetText("Showing JSON");
_answerView->SetText(lastJSON.c_str());
progressAnim=0;
} break;
_answerView->SetText(lastJSON.c_str());
progressAnim = 0;
} break;
case kSendReply: {
@ -395,14 +424,11 @@ void MainWindow::MessageReceived(BMessage *message) {
const char *text;
const char *fullJSON;
if (message->FindString("json", &fullJSON) == B_OK) {
lastJSON = std::string(fullJSON);
}
if (message->FindString("json", &fullJSON) == B_OK) {
lastJSON = std::string(fullJSON);
}
if (message->FindString("text", &text) == B_OK) {
// printf("Received text: %s\n", text);
@ -471,28 +497,26 @@ BMenuBar *MainWindow::_BuildMenu() {
item = new BMenuItem(B_TRANSLATE("Send Prompt" B_UTF8_ELLIPSIS),
new BMessage(kSendPrompt));
item->SetShortcut('S', B_COMMAND_KEY);
item->SetShortcut('S', B_COMMAND_KEY);
item->SetTarget(this);
menu->AddItem(item);
menu->AddSeparatorItem();
item = new BMenuItem(B_TRANSLATE("Clear History" B_UTF8_ELLIPSIS),
new BMessage(kClearHistory));
item->SetTarget(this);
item->SetShortcut('K', B_COMMAND_KEY | B_SHIFT_KEY);
item->SetShortcut('K', B_COMMAND_KEY | B_SHIFT_KEY);
menu->AddItem(item);
menuBar->AddItem(menu);
menu->AddSeparatorItem();
item = new BMenuItem(B_TRANSLATE("View full json" B_UTF8_ELLIPSIS),
menu->AddSeparatorItem();
item = new BMenuItem(B_TRANSLATE("View full json" B_UTF8_ELLIPSIS),
new BMessage(kViewJSON));
item->SetTarget(this);
item->SetShortcut('J', B_COMMAND_KEY | B_SHIFT_KEY);
item->SetShortcut('J', B_COMMAND_KEY | B_SHIFT_KEY);
menu->AddItem(item);
return menuBar;
}