def METHOD_NAME(api_port: Optional[int], api_key: Optional[str], root_state_dir, parsed_args):
    logger.info(f"Running GUI in {'gui_test_mode' if parsed_args.gui_test_mode else 'normal mode'}")
    # Workaround for macOS Big Sur, see https://github.com/Tribler/tribler/issues/5728
    if sys.platform == "darwin":
        logger.info('Enabling a workaround for macOS Big Sur')
        os.environ["QT_MAC_WANTS_LAYER"] = "1"
    # Workaround for Ubuntu 21.04+, see https://github.com/Tribler/tribler/issues/6701
    elif sys.platform == "linux":
        logger.info('Enabling a workaround for Ubuntu 21.04+ wayland environment')
        os.environ["GDK_BACKEND"] = "x11"
    current_process = TriblerProcess.current_process(ProcessKind.GUI)
    process_manager = ProcessManager(root_state_dir, current_process)
    set_global_process_manager(process_manager)  # to be able to add information about exception to the process info
    current_process_is_primary = process_manager.current_process.become_primary()
    load_logger_config('tribler-gui', root_state_dir, current_process_is_primary)
    # Enable tracer using commandline args: --trace-debug or --trace-exceptions
    trace_logger = check_and_enable_code_tracing('gui', root_state_dir)
    enable_fault_handler(root_state_dir)
    # Exit if we cant read/write files, etc.
    check_environment()
    check_free_space()
    try:
        app_name = os.environ.get('TRIBLER_APP_NAME', 'triblerapp')
        app = TriblerApplication(app_name, sys.argv, start_local_server=current_process_is_primary)
        app_manager = AppManager(app)
        # Note (@ichorid): translator MUST BE created and assigned to a separate variable
        # before calling installTranslator on app. Otherwise, it won't work for some reason
        settings = QSettings('nl.tudelft.tribler')
        translator = get_translator(settings.value('translation', None))
        app.installTranslator(translator)
        if not current_process_is_primary:
            logger.info('GUI Application is already running.')
            app.send_torrent_file_path_to_primary_process()
            logger.info('Close the current GUI application.')
            process_manager.sys_exit(1, 'Tribler GUI application is already running')
        logger.info('Start Tribler Window')
        window = TriblerWindow(process_manager, app_manager, settings, root_state_dir,
                               api_port=api_port, api_key=api_key)
        window.setWindowTitle("Tribler")
        app.tribler_window = window
        app.parse_sys_args(sys.argv)
        exit_code = app.exec_()
        process_manager.sys_exit(exit_code or None)
    except Exception as exc:  # pylint: disable=broad-except
        logger.exception(exc)
        show_system_popup("Tribler Exception", f"{exc.__class__.__name__}: {exc}")
        process_manager.sys_exit(1, exc)
    except SystemExit:
        logger.info("Shutting down Tribler")
        if trace_logger:
            trace_logger.close()
        # Flush all the logs to make sure it is written to file before it exits
        for handler in logging.getLogger().handlers:
            handler.flush()
        gui_sentry_reporter.global_strategy = SentryStrategy.SEND_SUPPRESSED
        raise