Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: run app on background #793

Open
bestagi opened this issue Jun 23, 2024 · 11 comments
Open

Feature request: run app on background #793

bestagi opened this issue Jun 23, 2024 · 11 comments
Assignees
Labels
enhancement: features 🚀 New feature or request

Comments

@bestagi
Copy link

bestagi commented Jun 23, 2024

Thanks for this awesome Apps, I use it every day!

But it will be great if this app can run in background when I click X button and show a little icon at corner of desktop system tray

@dongfengweixiao
Copy link
Contributor

Snipaste_2024-07-30_10-58-46
I am trying to implement this feature. How do you feel about using https://pub.dev/packages/tray_manager to provide support for the system tray functionality?@Feichtmeier

@Feichtmeier
Copy link
Member

Snipaste_2024-07-30_10-58-46 I am trying to implement this feature. How do you feel about using https://pub.dev/packages/tray_manager to provide support for the system tray functionality?@Feichtmeier

cool! I feel good with it :)
The only bad thing is that I am still struck with my home grown weird settings service
I think I should first port it to shared_prefrences so you can use it to store your setting?

@dongfengweixiao
Copy link
Contributor

Yes, I saw that you mentioned in another issue that you are migrating persistence to shared_preferences.

However, in the current architecture, saving the default behavior of the close button is easy. I have already done most of the work on the current code. I will commit all the code to GitHub later.

@Feichtmeier
Copy link
Member

Yes, I saw that you mentioned in another issue that you are migrating persistence to shared_preferences.

However, in the current architecture, saving the default behavior of the close button is easy. I have already done most of the work on the current code. I will commit all the code to GitHub later.

ok :) Thank you for contributing

@dongfengweixiao
Copy link
Contributor

Yes, I saw that you mentioned in another issue that you are migrating persistence to shared_preferences.
However, in the current architecture, saving the default behavior of the close button is easy. I have already done most of the work on the current code. I will commit all the code to GitHub later.

ok :) Thank you for contributing

图片

enum CloseBtnAction {
  none('always ask'),
  hide('hide to tray'),
  close('close');

  final String description;

  const CloseBtnAction(this.description);
  @override
  String toString() => name;
}
class _CloseActionSection extends StatelessWidget with WatchItMixin {
  const _CloseActionSection();

  @override
  Widget build(BuildContext context) {
    final model = di<SettingsModel>();

    final closeBtnAction =
        watchPropertyValue((SettingsModel m) => m.closeBtnActionIndex);
    return YaruSection(
      margin: const EdgeInsets.only(
        left: kYaruPagePadding,
        top: kYaruPagePadding,
        right: kYaruPagePadding,
      ),
      headline: Text(context.l10n.closeButtonAction),
      child: Column(
        children: [
          YaruTile(
            title: Text(context.l10n.closeButtonAction),
            trailing: YaruPopupMenuButton<CloseBtnAction>(
              icon: const DropDownArrow(),
              initialValue: closeBtnAction,
              child: Text(closeBtnAction.description),
              onSelected: (value) {
                model.setCloseBtnActionIndex(value);
              },
              itemBuilder: (context) {
                return [
                  for (var i = 0; i < CloseBtnAction.values.length; ++i)
                    PopupMenuItem(
                      value: CloseBtnAction.values[i],
                      child: Text(CloseBtnAction.values[i].description),
                    ),
                ];
              },
            ),
          ),
        ],
      ),
    );
  }
}

It seems impossible to implement i18n in enum. Do you have any good suggestions?

@Feichtmeier
Copy link
Member

enum CloseBtnAction {
  none('always ask'),
  hide('hide to tray'),
  close('close');

  final String description;

  const CloseBtnAction(this.description);
  @override
  String toString() => name;
}

yes, do it like this

enum CloseBtnAction {
   alwaysAsk,
   hideToTray,
   close;

  String localize(AppLocalizations l10n) => switch (this) {
        alwaysAsk => l10n.alwaysAsk, // <- create this in the english and (chinese?) arb files
        hideToTray => l10n.hideToTray, // <- create this in the english and (chinese?) arb files
        close => l10n.close,
      };

}

you can regen the arb files either from this button in vscode
grafik
or from the command "Flutter: Generate Localizations" in the command prompt in vscode

@Feichtmeier
Copy link
Member

@dongfengweixiao it works in the snap :)

However for Linux snap and flatpak we need to set the icon differently

Feichtmeier added a commit that referenced this issue Aug 22, 2024
Feichtmeier added a commit that referenced this issue Aug 22, 2024
@Feichtmeier
Copy link
Member

damn this also didnt help @dongfengweixiao
any idea to make the icon show for snaps and flatpaks?

@dongfengweixiao
Copy link
Contributor

let me look...

@dongfengweixiao
Copy link
Contributor

String _icon() {
if (Platform.isLinux) {
return kAppId;
} else if (Platform.isWindows) {
return 'assets/images/tray_icon.ico';
} else {
return 'assets/images/tray_icon.png';
}
}

The existing code causes MusicPod to not display the tray icon on Arch Linux. It may be necessary to determine whether the current program is running in a sandbox environment.

However, I have not yet verified whether this part of the code works effectively in a flatpak or snap environment.

bool isFlatpakEnv() {
  return File('/.flatpak-info').existsSync();
}

String _icon() {
  if (isFlatpakEnv() || Platform.environment.containsKey('SNAP_NAME')) {
    return kAppId;
  } else if (Platform.isWindows) {
    return 'assets/images/tray_icon.ico';
  } else {
    return 'assets/images/tray_icon.png';
  }
}

# - run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libmpv-dev libappindicator3-1 libappindicator3-dev

Like in release.yml, maybe snap and flatpak need to add libayatana-appindicator3 as a runtime dependency and add libayatana-appindicator3-dev as a build-time dependency.


@override
void onWindowEvent(String eventName) {
if ('show' == eventName || 'hide' == eventName) {
updateTrayItems(context);
}
super.onWindowEvent(eventName);
}

Directly calling updateTrayItems in app.dart will cause a _TypeError (Null check operator used on a null value) error. However, in the release version of the application, this does not affect the functionality of the tray (although you cannot switch between hide or display string only). I think I have found a way to solve this problem. I will separate the system tray implementation into a service, which will listen to the window state.

However, these changes need to be completed next week. I'm going on a trip this weekend to visit one of the filming locations of "Black Myth: Wukong," Mount Wutai in Shanxi, China.

@Feichtmeier
Copy link
Member

Feichtmeier commented Aug 25, 2024

Hey guys I rethought about this:

I spent quite some time on using the actual media trays on macos, linux and windows. So there is already a media tray for music players we already use.
It is confusing and doesnt have the same functionalities as the native media trays
Plus even if it would not be confusing, we didnt figure out the icon situation yet

Running the app in the background is a different thing but also has some edge cases we didnt thought about yet:

  • on linux musicpod can be used to open music files from the filemanager, if the app is hidden to tray, this does not work
  • we didnt test how this behaves on windows and macos yet at all

Until we didnt figure this all out I will remove this from main again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: features 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants