1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:27:43 +00:00

Ladybird/AppKit: Implement the <input type=file> UI

This commit is contained in:
Timothy Flynn 2024-02-25 14:12:21 -05:00 committed by Andreas Kling
parent 834f76ef24
commit a346f14fb4
3 changed files with 45 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <AK/URL.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibWeb/HTML/SelectedFile.h>
#include <LibWebView/SearchEngine.h>
#include <LibWebView/SourceHighlighter.h>
#include <LibWebView/URL.h>
@ -612,6 +613,42 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
[panel makeKeyAndOrderFront:nil];
};
m_web_view_bridge->on_request_file_picker = [self](auto allow_multiple_files) {
auto* panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:NO];
if (allow_multiple_files == Web::HTML::AllowMultipleFiles::Yes) {
[panel setAllowsMultipleSelection:YES];
[panel setMessage:@"Select files"];
} else {
[panel setAllowsMultipleSelection:NO];
[panel setMessage:@"Select file"];
}
[panel beginSheetModalForWindow:[self window]
completionHandler:^(NSInteger result) {
Vector<Web::HTML::SelectedFile> selected_files;
auto create_selected_file = [&](NSString* ns_file_path) {
auto file_path = Ladybird::ns_string_to_byte_string(ns_file_path);
if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error())
warnln("Unable to open file {}: {}", file_path, file.error());
else
selected_files.append(file.release_value());
};
if (result == NSModalResponseOK) {
for (NSURL* url : [panel URLs]) {
create_selected_file([url path]);
}
}
m_web_view_bridge->file_picker_closed(move(selected_files));
}];
};
self.select_dropdown = [[NSMenu alloc] initWithTitle:@"Select Dropdown"];
[self.select_dropdown setDelegate:self];

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/ByteString.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibGfx/Color.h>
@ -18,6 +19,7 @@
namespace Ladybird {
String ns_string_to_string(NSString*);
ByteString ns_string_to_byte_string(NSString*);
NSString* string_to_ns_string(StringView);
NSData* string_to_ns_data(StringView);

View file

@ -14,6 +14,12 @@ String ns_string_to_string(NSString* string)
return MUST(String::from_utf8({ utf8, strlen(utf8) }));
}
ByteString ns_string_to_byte_string(NSString* string)
{
auto const* utf8 = [string UTF8String];
return ByteString(utf8, strlen(utf8));
}
NSString* string_to_ns_string(StringView string)
{
return [[NSString alloc] initWithData:string_to_ns_data(string) encoding:NSUTF8StringEncoding];