updated ytsearchbar, bugs when reopening the ytsearchbar

This commit is contained in:
liyaowhen
2024-08-04 21:53:45 -07:00
parent 8db7622148
commit e0c8fa374b
2 changed files with 79 additions and 61 deletions
+19 -1
View File
@@ -7,6 +7,8 @@ namespace Song {
public PlaylistObject current_playlist;
private Gtk.Revealer add_button_revealer = new Gtk.Revealer();
private Gtk.Revealer controls_revealer = new Gtk.Revealer();
private Gtk.Revealer exit_yt_search_mode_revealer = new Gtk.Revealer();
private Adw.HeaderBar action_bar;
@@ -46,11 +48,22 @@ namespace Song {
popover.present(this);
});
var action_bar = new Adw.HeaderBar();
var exit_yt_search_mode = new Gtk.Button.from_icon_name("go-previous-symbolic");
exit_yt_search_mode.valign = Gtk.Align.CENTER;
exit_yt_search_mode_revealer.set_child(exit_yt_search_mode);
exit_yt_search_mode_revealer.set_reveal_child(false);
exit_yt_search_mode_revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_RIGHT;
exit_yt_search_mode.clicked.connect(() => {
SignalHub.get_instance().exit_yt_search_mode();
});
action_bar = new Adw.HeaderBar();
action_bar.show_back_button = false;
action_bar.title_widget = new YtSearchBar();
action_bar.pack_start(collapse_button);
action_bar.pack_start(add_button_revealer);
action_bar.pack_start(exit_yt_search_mode_revealer);
action_bar.margin_start = 5;
collapse_button.clicked.connect(() => {
@@ -100,11 +113,16 @@ namespace Song {
signal_hub.enter_yt_search_mode.connect(() => {
add_button_revealer.set_reveal_child(false);
controls_revealer.set_reveal_child(false);
exit_yt_search_mode_revealer.set_reveal_child(true);
action_bar.set_margin_start(-5);
});
signal_hub.exit_yt_search_mode.connect(() => {
add_button_revealer.set_reveal_child(true);
controls_revealer.set_reveal_child(true);
exit_yt_search_mode_revealer.set_reveal_child(false);
action_bar.set_margin_start(0);
});
}
+60 -60
View File
@@ -66,41 +66,17 @@ namespace Song {
private void instanciate_signals() {
var signal_hub = SignalHub.get_instance();
signal_hub.mouse_clicked.connect(() => {
if (!isMouseInside && isEnlarged) {
//shrink();
//signal_hub.exit_yt_search_mode();
} else if (!isEnlarged && isMouseInside) {
enlarge();
signal_hub.enter_yt_search_mode();
} else if (!isEnlarged && !isMouseInside && state == YtSearchBarStates.ENLARGING) {
requesting_state = YtSearchBarStates.SHRINKING;
Timeout.add(1, () => {
if (requesting_state != YtSearchBarStates.SHRINKING) return false;
if (isEnlarged) {
shrink();
signal_hub.exit_yt_search_mode();
requesting_state = null;
return false;
}
return true;
});
} else if (isEnlarged && isMouseInside && state == YtSearchBarStates.SHRINKING) {
requesting_state = YtSearchBarStates.ENLARGING;
Timeout.add(1, () => {
if (requesting_state != YtSearchBarStates.ENLARGING) return false;
if (!isEnlarged) {
enlarge();
signal_hub.enter_yt_search_mode();
requesting_state = null;
return false;
}
return true;
});
}
});
search_entry.changed.connect((i) => {
if (i.text != "") {
if (state != YtSearchBarStates.ENLARGING && isEnlarged != true && requesting_state != YtSearchBarStates.ENLARGING) {
enlarge();
signal_hub.enter_yt_search_mode();
}
} else {
shrink();
signal_hub.exit_yt_search_mode();
}
signal_hub.request_search(i.get_text());
});
}
@@ -135,19 +111,20 @@ namespace Song {
public class YtSearchView : Gtk.Box {
private Gtk.Label label = new Gtk.Label("");
private YtSearchViewStates state = YtSearchViewStates.IDLE;
private Cancellable active_search_query;
private Gee.HashSet<YtSearchItem> results = new Gee.HashSet<YtSearchItem>();
private Gee.HashSet<Gtk.Widget> result_widgets = new Gee.HashSet<Gtk.Widget>();
private Gtk.FlowBox flow_box = new Gtk.FlowBox();
private Subprocess? search_process = null;
construct {
orientation = Gtk.Orientation.VERTICAL;
append(label);
append(flow_box);
flow_box.set_max_children_per_line(4);
instanciate_signals();
@@ -170,6 +147,11 @@ namespace Song {
motion_sensor.enter.connect(() => {
signal_hub.yt_search_bar.isMouseInside = true;
});
hexpand = true;
hexpand_set = true;
vexpand = true;
vexpand_set = true;
}
private void instanciate_signals() {
@@ -179,11 +161,11 @@ namespace Song {
state = YtSearchViewStates.SEARCHING;
});
label.label = signal_hub.yt_search_bar.search_entry.text;
//label.label = signal_hub.yt_search_bar.search_entry.text;
}
private async void search(string query) {
label.label = query;
//label.label = query;
string temp_file = Path.build_path("/", Environment.get_tmp_dir(), "yt_search_tmp.json");
string[] command = {
@@ -200,11 +182,6 @@ namespace Song {
MainLoop loop = new MainLoop ();
try {
GLib.Pid child_pid;
int std_out;
int std_input;
int std_error;
//TODO: remove ghost processes
@@ -232,7 +209,7 @@ namespace Song {
out std_error
);*/
if (search_process != null && !search_process.get_if_exited()) {
if (search_process != null && search_process.get_identifier() != null) {
search_process.force_exit();
}
try {
@@ -464,35 +441,37 @@ namespace Song {
results.foreach((result) => {
var label = new Gtk.Label(result.title);
var link = new Gtk.LinkButton(result.url);
var thumbnail = new Gtk.Image();
thumbnail.hexpand = true;
thumbnail.hexpand_set = true;
var thumbnail = new Gtk.Picture();
thumbnail.vexpand = true;
thumbnail.vexpand_set = true;
thumbnail.icon_size = Gtk.IconSize.LARGE;
load_url_to_image(thumbnail, result.thumbnail_url);
thumbnail.can_shrink = false;
load_url_to_image.begin(thumbnail, result.thumbnail_url);
var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 5);
var options_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 5);
options_box.append(label);
box.append(thumbnail);
box.append(options_box);
append(box);
result_widgets.add(box);
var overlay = new Gtk.Overlay();
overlay.set_child(thumbnail);
overlay.add_overlay(options_box);
append(overlay);
result_widgets.add(overlay);
return true;
});
}
private async void load_url_to_image (Gtk.Image? image, string url) {
private async void load_url_to_image (Gtk.Picture? image, string url) {
Soup.Session session = new Soup.Session ();
Soup.Message message = new Soup.Message ("GET", url);
GLib.Bytes bytes = session.send_and_read(message, null);
var stream = new GLib.MemoryInputStream();
stream.add_bytes(bytes);
var pixbuf = new Gdk.Pixbuf.from_stream(stream, null);
if (image != null) {
image.set_from_pixbuf(pixbuf);
}
session.send_and_read_async.begin(message, 1, null, (o, object) => {
var bytes = session.send_and_read_async.end(object);
var stream = new GLib.MemoryInputStream();
stream.add_bytes(bytes);
var pixbuf = new Gdk.Pixbuf.from_stream(stream, null);
if (image != null) {
image.set_pixbuf(pixbuf);
}
});
}
}
@@ -501,4 +480,25 @@ namespace Song {
public string url = "";
public string thumbnail_url = "";
}
public class ExpandableImage : Gtk.DrawingArea {
public Gdk.Pixbuf? pixbuf = null;
public ExpandableImage () {
var frame = new Gtk.Frame(null);
this.set_draw_func((drawing_area, cairo_context, i) => {
if (pixbuf != null) {
Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, 0, 0);
} else {
cairo_context.set_source_rgb(200, 200, 200);
}
cairo_context.paint();
});
queue_draw();
}
}
}