|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 MarcusPy827 |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <QVBoxLayout> |
| 19 | +#include <QHBoxLayout> |
| 20 | +#include <QSpacerItem> |
| 21 | +#include <QProcess> |
| 22 | +#include <QRegularExpression> |
| 23 | +#include <QMouseEvent> |
| 24 | +#include <QFontMetrics> |
| 25 | +#include <QSizePolicy> |
| 26 | +#include <QMargins> |
| 27 | + |
| 28 | +#include <string> |
| 29 | + |
| 30 | +#include "absl/log/log.h" |
| 31 | +#include "absl/log/check.h" |
| 32 | +#include "absl/strings/str_format.h" |
| 33 | +#include "absl/strings/str_cat.h" |
| 34 | + |
| 35 | +#include "src/components/app_drawer/app_drawer_search_item.h" |
| 36 | + |
| 37 | +namespace panel { |
| 38 | +namespace frontend { |
| 39 | + |
| 40 | +AppDrawerSearchItem::AppDrawerSearchItem(const AppInfo& info, QWidget |
| 41 | + *parent) : QWidget(parent) { |
| 42 | + this->info_ = info; |
| 43 | + this->setProperty("class", "app_drawer_search_item"); |
| 44 | + this->setCursor(Qt::PointingHandCursor); |
| 45 | + |
| 46 | + QHBoxLayout * layout_gen = new QHBoxLayout; |
| 47 | + layout_gen->setContentsMargins(0, 0, 0, 0); |
| 48 | + layout_gen->setSpacing(6); |
| 49 | + layout_gen->setAlignment(Qt::AlignLeft | Qt::AlignTop); |
| 50 | + this->setLayout(layout_gen); |
| 51 | + |
| 52 | + if (btn_ == nullptr) { |
| 53 | + btn_ = new QToolButton(); |
| 54 | + btn_->setProperty("class", "drawer_item_btn"); |
| 55 | + btn_->setIconSize(QSize(48, 48)); |
| 56 | + btn_->setMinimumSize(QSize(48, 48)); |
| 57 | + } |
| 58 | + btn_->setAttribute(Qt::WA_TransparentForMouseEvents, true); |
| 59 | + btn_->setIcon(QIcon::fromTheme(info_.icon_name)); |
| 60 | + layout_gen->addWidget(btn_); |
| 61 | + |
| 62 | + QVBoxLayout * info_layout = new QVBoxLayout; |
| 63 | + info_layout->setContentsMargins(0, 0, 0, 0); |
| 64 | + info_layout->setSpacing(0); |
| 65 | + layout_gen->addLayout(info_layout); |
| 66 | + |
| 67 | + if (title_ == nullptr) { |
| 68 | + title_ = new QLabel(); |
| 69 | + title_->setProperty("class", "app_drawer_item_label"); |
| 70 | + title_->setAttribute(Qt::WA_TransparentForMouseEvents, true); |
| 71 | + } |
| 72 | + title_->setText("<b>" + info_.name + "</b>"); |
| 73 | + info_layout->addWidget(title_); |
| 74 | + |
| 75 | + if (!info_.comment.isEmpty()) { |
| 76 | + if (comment_ == nullptr) { |
| 77 | + comment_ = new QLabel(); |
| 78 | + comment_->setProperty("class", "app_drawer_item_label"); |
| 79 | + comment_->setAttribute(Qt::WA_TransparentForMouseEvents, true); |
| 80 | + comment_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 81 | + comment_->setMinimumWidth(0); |
| 82 | + comment_->setWordWrap(false); |
| 83 | + } |
| 84 | + raw_comment_ = info_.comment; |
| 85 | + comment_->setToolTip(raw_comment_); |
| 86 | + info_layout->addWidget(comment_); |
| 87 | + UpdateCommentElide(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +AppDrawerSearchItem::~AppDrawerSearchItem() { |
| 92 | + LOG(INFO) << absl::StrCat("AppDrawerSearchItem is being deleted."); |
| 93 | +} |
| 94 | + |
| 95 | +void AppDrawerSearchItem::mousePressEvent(QMouseEvent *event) { |
| 96 | + if (event->button() == Qt::LeftButton) { |
| 97 | + Launch(); |
| 98 | + event->accept(); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + QWidget::mousePressEvent(event); |
| 103 | +} |
| 104 | + |
| 105 | +void AppDrawerSearchItem::resizeEvent(QResizeEvent *event) { |
| 106 | + QWidget::resizeEvent(event); |
| 107 | + UpdateCommentElide(); |
| 108 | +} |
| 109 | + |
| 110 | +void AppDrawerSearchItem::showEvent(QShowEvent *event) { |
| 111 | + QWidget::showEvent(event); |
| 112 | + UpdateCommentElide(); |
| 113 | +} |
| 114 | + |
| 115 | +void AppDrawerSearchItem::UpdateCommentElide() { |
| 116 | + if (comment_ == nullptr || raw_comment_.isEmpty()) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const auto *layout_gen = qobject_cast<QHBoxLayout*>(layout()); |
| 121 | + int available_width = comment_->width() * 0.8; |
| 122 | + if (layout_gen != nullptr) { |
| 123 | + const QMargins margins = layout_gen->contentsMargins(); |
| 124 | + available_width = this->width() - margins.left() - margins.right() - |
| 125 | + btn_->width() - layout_gen->spacing(); |
| 126 | + available_width *= 0.95; |
| 127 | + } |
| 128 | + |
| 129 | + if (available_width <= 0) { |
| 130 | + comment_->setText(raw_comment_); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + const QFontMetrics metrics(comment_->font()); |
| 135 | + comment_->setText(metrics.elidedText(raw_comment_, Qt::ElideRight, |
| 136 | + available_width)); |
| 137 | +} |
| 138 | + |
| 139 | +void AppDrawerSearchItem::Launch() { |
| 140 | + LOG(INFO) << absl::StrFormat("Launching application: %s", |
| 141 | + info_.name.toStdString()); |
| 142 | + |
| 143 | + if (info_.exec.isEmpty()) { |
| 144 | + LOG(ERROR) << absl::StrCat( |
| 145 | + "No executable command found for application: ", |
| 146 | + info_.name.toStdString(), ". Launch aborted."); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + QString raw_exec = info_.exec; |
| 151 | + static const QRegularExpression field_regex( |
| 152 | + QStringLiteral("\\s%[fFuUdDnNickvm]")); |
| 153 | + QString clean_exec = raw_exec.replace(field_regex, QString()); |
| 154 | + |
| 155 | + std::string cmd = "(" + clean_exec.toStdString() + ") &"; |
| 156 | + int state = std::system(cmd.c_str()); |
| 157 | + if (state != 0) { |
| 158 | + LOG(ERROR) << absl::StrCat( |
| 159 | + "Failed to launch application: ", info_.name.toStdString(), |
| 160 | + " with command: ", clean_exec.toStdString()); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void AppDrawerSearchItem::UpdateFilter(const QString& keyword) { |
| 165 | + bool should_show = false; |
| 166 | + if (info_.name.contains(keyword, Qt::CaseInsensitive)) { |
| 167 | + should_show = true; |
| 168 | + } else if (info_.generic_name.contains(keyword, Qt::CaseInsensitive)) { |
| 169 | + should_show = true; |
| 170 | + } else if (info_.comment.contains(keyword, Qt::CaseInsensitive)) { |
| 171 | + should_show = true; |
| 172 | + } |
| 173 | + |
| 174 | + if (should_show) { |
| 175 | + if (!this->isVisible()) { |
| 176 | + this->setVisible(true); |
| 177 | + } |
| 178 | + } else { |
| 179 | + if (this->isVisible()) { |
| 180 | + this->setVisible(false); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +} // namespace frontend |
| 186 | +} // namespace panel |
0 commit comments