// Copyright (C) 2023 Tasuku Suzuki <tasuku.suzuki@signal-slot.co.jp>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"

#include <functional>

QT_BEGIN_NAMESPACE
class QObject;
class QWidget;
QT_END_NAMESPACE

namespace Utils {

QTCREATOR_UTILS_EXPORT void setWheelScrollingWithoutFocusBlocked(QWidget *widget);

// Calls func() once, just before widget is shown for the first time, or
// immediately if it is visible already. Lets a widget keep its construction
// cheap and build the expensive content that only matters on screen later.
// Settings page widgets in particular are also constructed - and never shown -
// just to harvest their search keywords.
QTCREATOR_UTILS_EXPORT void onFirstShow(QWidget *widget, const std::function<void()> &func);

QTCREATOR_UTILS_EXPORT QWidget *dialogParent();
QTCREATOR_UTILS_EXPORT void setDialogParentGetter(QWidget *(*getter)());

// Whether a user is available to answer blocking dialogs. A scripted/headless
// session (e.g. driven over the MCP server) sets this to false so code that
// would otherwise wait on a modal dialog proceeds with a sensible default
// instead of blocking forever. Default: true.
QTCREATOR_UTILS_EXPORT void setDialogsInteractive(bool interactive);
QTCREATOR_UTILS_EXPORT bool dialogsInteractive();

// returns previous value
QTCREATOR_UTILS_EXPORT bool setIgnoreForDirtyHook(QObject *object, bool ignore = true);
QTCREATOR_UTILS_EXPORT bool isIgnoredForDirtyHook(const QObject *object);

QTCREATOR_UTILS_EXPORT void markSettingsDirty();
QTCREATOR_UTILS_EXPORT void checkSettingsDirty();

// Disables use of dirty hooks while active. Reference-counted, so overlapping
// guards (even from different threads) no longer strand the suppression state.
class QTCREATOR_UTILS_EXPORT DirtySettingsGuard
{
public:
    DirtySettingsGuard();
    ~DirtySettingsGuard();
};

QTCREATOR_UTILS_EXPORT void installCheckSettingsDirtyTrigger(QObject *object);

QTCREATOR_UTILS_EXPORT void installMarkSettingsDirtyTrigger(QObject *object);
QTCREATOR_UTILS_EXPORT void installMarkSettingsDirtyTriggerRecursively(QObject *object); // Avoid.

namespace Internal {
QTCREATOR_UTILS_EXPORT void setMarkSettingsDirtyHook(const std::function<void (bool)> &hook);
QTCREATOR_UTILS_EXPORT void setCheckSettingsDirtyHook(const std::function<void ()> &hook);
}

} // namespace Utils
